UITableviewcell with Right Detail - TextLabel.text is always NULL - ios

It's just very basic thing, I am using UITableview with cell style "Right Detail"
Now, in method cellForRowAtIndexPath when I do
cell.textLabel.text = #"Notification";
cell.detailTextLabel.text = #"Dates";
It never takes the text neither displays on the screen. When I log the cell text like
NSLog(#"%# -- %#",cell.textLabel.text, cell.detailTextLabel.text);
It prints the following line in console
(null) -- (null)
Here are some more details:
Xcode Version: 8.3.1
Simulator: iPad Pro (12.9 inch) iOS 10.3
Storyboard based tableview
Already selected "Right Detail" option from Style option.
Already tested the following
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifier]
On the requests, here is the complete code for "CellForRowAtIndexpath"
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = nil;
if (tableView == self.tableviewBusinessSettings) {
NSString *identifier = #"BCell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier];\
NSDictionary *dictSetting = [self.arrayBusinessSettings objectAtIndex:indexPath.row];
[cell.textLabel setText:[dictSetting objectForKey:#"key"]];
[cell.detailTextLabel setText:[dictSetting objectForKey:#"value"]];
}else if (tableView == self.tableviewNotifications) {
NSString *identifier = #"NCell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier];
cell.textLabel.text = #"Notification";
cell.detailTextLabel.text = #"Dates";
}
NSLog(#"%# -- %#",cell.textLabel.text, cell.detailTextLabel.text);
return cell;
}
Need quick help.
Thanks

Try checking the data in dictionary Like before sending it to cells of tableView just nslog() the data you are sending and other Thing try using
cell.textlabel.text = [dictSetting objectForKey:#"key"];
cell.detailTextLabel.text = [dictSetting objectForKey:#"value"];
and try replacing
UITableViewCell *cell = nil; to UITableViewCell *cell;

Related

Cropped Text on TableViewController

(iOS 6.1.6, iPod Touch 4th Gen, Xcode 7)
Using the default "Subtitle" format for a TableViewController, I'm getting data that looks like this. Scrolling will sometimes refresh so it shows correctly, but not always. I've tried widening the labels in the prototype cell, but that doesn't seem to make a difference.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Creating Cell");
static NSString *tableID = #"locationOverviewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableID];
}
cell.textLabel.text = ((PullLocation *)WarehouseLocations[indexPath.row]).WarehouseLocation;
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d Items To Pull",(int)((PullLocation *)WarehouseLocations[indexPath.row]).NumItemsToPull];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Can anyone point me in the right direction?

UItableviewcell custom cell not showing details

Any help please!! I am trying to show title and details, but details do not display(blank).
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
}
NSString * theCell = [NSString stringWithFormat:#"From %# to %# at %#", [flights valueForKey:#"depAirport"],[flights valueForKey:#"destAirport"],[flights valueForKey:#"depTime"]];
NSLog(#"the %#", theCell);
// Configure the cell...
// Configure the cell...
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:12];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.textColor =[UIColor blackColor];
cell.textLabel.text = [[flights objectAtIndex:[indexPath row]] valueForKey:#"flight_ID"];
cell.detailTextLabel.text = theCell;
return cell;
}
Any help would be appreciate!!
If you are using a storyboard, your cell not become nil. That means cell is always returned from dequeue method. So your cell initialization code never get initialized and the style doesn't change to UITableViewCellStyleSubtitle. Therefore detail text label doesnt exist.
So you have to change the prototype in the storyboard to have the subtitle style.
What UITableViewCellStyle are you using? UITableViewCellStyleDefault will not work.

iOS 7 UITableViewCell issue

I have an iOS 6 app that I am converting to iOS 7 using Xcode 5 in StoryBoard. Everything worked fine until I decided I wanted to add a custom UITableViewCell to a tableview controller after converting the app to iOS 7. I created the usual code to do this but when I run the app it gives an unexpected result whereby it just displays the contents of one cell row and it sort of floats above the tableview which shows what appears to be empty cell rows behind it. When I scroll the table rows, I see them scrolling in the background and the data being shown in the one view cell row changes while scrolling (see image attached). However, if I click on what looks like an empty row, it works correctly and it segues correctly to the detail view controller. I made the same changes to the app in iOS 6 using Xcode 4.6 on my other macbook and it works correctly (see image attached). So I'm thinking that iOS 7 handles the uitableviewcell class differently than in iOS 6 or I am making a mistake in the code in the iOS 7 app and not realizing it.
Here is the iOS 7 code that's not working correctly:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
ExhibitorsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[ExhibitorsViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (tableView == self.myTableView){
NSManagedObject *object = [self.objects objectAtIndex:indexPath.row];
cell.nameLabel.text = [object valueForKey:#"name"];
NSString * booth = [NSString stringWithFormat:#"Booth Number: %#", [object valueForKey:#"boothLabel"]];
cell.boothNumberLabel.text = booth;
}
else{
NSManagedObject *object = [results objectAtIndex:indexPath.row];
cell.nameLabel.text = [object valueForKey:#"name"];
NSString * booth = [NSString stringWithFormat:#"Booth Number: %#", [object valueForKey:#"boothLabel"]];
cell.boothNumberLabel.text = booth;
}
return cell;
}
Here is the iOS 6 code that works (to me it seems to be the same, but I guess I need other eyes to view it):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"Cell";
ExhibitorsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (!cell) {
cell = [[ExhibitorsViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (tableView == self.myTableView){
NSManagedObject *object = [self.objects objectAtIndex:indexPath.row];
cell.nameLabel.text = [object valueForKey:#"name"];
NSString * booth = [NSString stringWithFormat:#"Booth Number: %#", [object valueForKey:#"boothLabel"]];
cell.boothNumberLabel.text = booth;
}
else{
NSManagedObject *object = [results objectAtIndex:indexPath.row];
cell.nameLabel.text = [object valueForKey:#"name"];
NSString * booth = [NSString stringWithFormat:#"Booth Number: %#", [object valueForKey:#"boothLabel"]];
cell.boothNumberLabel.text = booth;
}
return cell;
}
Here is the screen shot of what's happening in the iOS 7 app.
Here is what it should look like (this is before I made the custom UITableViewCell changes).
I figured it out. I had to drag the uilabel a little above the uitableviewcell until I see the cell boundaries highlighted. This placed the label above the cell. I then had to move the label back into the viewcell. Never had to do that in xcode 4.6. But, oh well.

UITableViewCell subtitle. Not getting it to work

Using Xcode 4.6, I am trying to display a typical UITableView with its cells with subtitles.
If I am not wrong, the code is this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
Test *myTest = [self listTests][indexPath.row];
cell.textLabel.text = [myTest name];
UIFont *cellFont = [UIFont systemFontOfSize:16.0];
cell.textLabel.font = cellFont;
UIFont *detailFont = [UIFont systemFontOfSize:12.0];
NSMutableString *detailText = [NSMutableString stringWithFormat:#"%d", [myTest numQuestions]];
[detailText appendString:#" preguntas."];
cell.detailTextLabel.text = detailText;
cell.detailTextLabel.font = detailFont;
return cell;
}
For some reason, it never passes through this line of code:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
so the cell is never initialized with UITableViewCellStyleSubtitle.
It is somehow getting ALWAYS FROM THE BEGINING a valid cell when doing [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
What can I be doing wrong?
It is really weard. I always use this code and it normally works. What else can I be doing wrong somewhere else?
This happens when your cell is defined using a storyboard prototype. In this case the reusable cells are pre-created using the initWithCoder: method, so if (cell == nil) never gets hit. See this question for more information.
Since it appears that you would like to use a cell with a standard style, changing the table to not use a storyboard prototype or setting the prototype to "Subtitle" should fix this problem.

iOS Memory Leak cellForRowAtIndexPath

The leak instrument points to "cell.textLabel.text = str;" as a memory leak. I am not sure why since I autoreleased the cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
// Try to retrieve from the table view a now-unused cell with the given identifier.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
// If no cell is available, create a new one using the given identifier.
if (cell == nil) {
// Use the default cell style.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
// Set up the cell.
NSString *str = [array objectAtIndex:indexPath.row];
cell.textLabel.text = str;
return cell;
}
you might not have released the array object that you use for getting the Strings.Also , try casting the value after extracting from array as
str= [NSString stringWithFormat:"%#",(NSString *)[array objectAtIndex:indexPath.row]];

Resources