15298

tableView reloadData not working: cellForRowAtIndexPath not called

Question:

I have this problem cellForRowAtIndexPath is not called by reloadingData. I have spent a lot of time with this. I xib file dataSource and delegate are connected to File's Owner. Any idea is welcome.

This is my code :

<em><strong></strong></em>*<em>cellForRowAtIndexPath</em><strong><em>*</em></strong>

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.backgroundColor = [UIColor clearColor]; cell.selectionStyle = UITableViewCellSelectionStyleGray; cell.backgroundView.opaque = NO; //cell.alpha = 0.65; cell.textLabel.backgroundColor = [UIColor clearColor]; cell.textLabel.opaque = NO; cell.textLabel.textColor = [UIColor whiteColor]; cell.textLabel.highlightedTextColor = [UIColor whiteColor]; cell.textLabel.font = [UIFont boldSystemFontOfSize:18]; cell.detailTextLabel.backgroundColor = [UIColor clearColor]; cell.detailTextLabel.opaque = NO; cell.detailTextLabel.textColor = [UIColor whiteColor]; cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor]; cell.detailTextLabel.font = [UIFont systemFontOfSize:14]; NSString *temp = [distanceArray objectAtIndex:indexPath.row]; if([checkMarkArray count] != 0){ NSString *checkString = [checkMarkArray objectAtIndex:0]; NSLog(@" checkString %@",checkString); if ([temp isEqualToString:checkString ]) { cell.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.png"]] autorelease]; } else { cell.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]] autorelease]; } } } // Set up the cell... [[cell textLabel] setText: [distanceArray objectAtIndex:indexPath.row]] ; return cell; }

<em><strong></strong></em><em>didSelectRowAtIndexPath</em><strong><em>*</em>***</strong>

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; cellText = selectedCell.textLabel.text; NSLog(@"%@",cellText); [checkMarkArray removeAllObjects]; if([[tableViewDistance cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark) { //global array to store selected object [checkMarkArray removeObject:[distanceArray objectAtIndex:indexPath.row]]; NSLog(@"array %@",checkMarkArray); [tableViewDistance cellForRowAtIndexPath:indexPath].accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]] autorelease]; [[tableViewDistance cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone]; [self.tableViewDistance reloadData]; } else { [checkMarkArray addObject:[distanceArray objectAtIndex:indexPath.row]]; NSLog(@"array again %@",checkMarkArray); [tableViewDistance cellForRowAtIndexPath:indexPath].accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.png"]] autorelease]; [[tableViewDistance cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark]; [self.tableViewDistance reloadData]; } tableViewDistance.delegate = self; tableViewDistance.dataSource = self; [self.tableViewDistance reloadData]; }

*<strong><em>viewDidLoad</em>**<em>*</em></strong>

- (void)viewDidLoad { distanceArray= [[NSMutableArray alloc] initWithObjects:@"20 Kilomètres", @"40 Kilomètres", @"60 Kilomètres",@"80 Kilomètres",nil]; NSLog(@"distance %@",distanceArray); tableViewDistance.backgroundColor=[UIColor clearColor]; checkMarkArray = [[NSMutableArray alloc] init]; [super viewDidLoad]; // Do any additional setup after loading the view from its nib. }

Answer1:

I've found some weak parts in your code:

<ol><li>

You should set cell.accessoryView out of if (cell == nil){}

</li> <li>

In - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath you can use tableView and not use tableViewDistance (may be it is nil?)

</li> <li>

In - (void)viewDidLoad you should call first [super viewDidLoad]; and then make customizations.

</li> </ol>

Hope that will help you. Gl

Answer2:

connect Your tableView outlet (UITableView *tableViewDistance) to Your Xib TableView.

Answer3:

Try to call

[tableView reloadData];

instead of

[self.tableViewDistance reloadData];

Answer4:

Based on your comment to the question, you want to have only one cell selected at a time, so you want to deselect the previously selected cell and select the one currently selected. You can use the -indexPathForSelectedRow method of UITableView to find the selected row:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // For multiselect, use an array to store the checked state instead of using the tableView's state. [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow]]; [self.tableView cellForRowAtIndexPath:indexPath].accessoryView = ...; }

You will also want to implement tableView:didDeselectRowAtIndexPath::

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { [self.tableView cellForRowAtIndexPath:indexPath].accessoryView = ...; }

Recommend

  • UITableViewCell Editing Minus Sign Frame?
  • Change one part of the selection with Regex
  • Returning view to original position after pan gesture, swift
  • collectionview change cell background image when scrolling
  • is there somebody who made an application using background for tableview and tableviewcell?
  • Setting background image for PFQueryTableViewController
  • Changing specific cells in uitableview
  • geom_line : How to connect only a few points
  • Tableview showing the wrong size of cell [duplicate]
  • Potential Leak, assigning NSString-Property to UILabel
  • SQL Server Cast and Rounding
  • d3.js Add labels in Chord diagram
  • tableView reloadData not working: cellForRowAtIndexPath not called
  • initial-scale = 1.0 + width= device-width not fitting the whole screen on any mobile
  • Equal strings not equal? [duplicate]
  • Use facet_grid option to plot column of dataframe with ggplot2
  • iPhone SDK: custom buttons in TableView
  • Perform Segue after clicking on search result
  • How can I animate a UITableViewCell's colors?
  • How to access the `detailTextLabel` in a `tableViewCell` at UITests?
  • changing uifont color in a table cell
  • Animate UILabel width with fixed center
  • UITableView cell are overlapping
  • -[NSCFString objectAtIndex:]: unrecognized selector
  • BackgroundColor of UITableViewCellStyleSubtitle labels?
  • Go back to previous view controller with help of swipe back (to the left)
  • Access textfield of UIAlertController - swift ios8
  • Custom ttf font makes Spritekit's transition slow in SWIFT
  • Solid Shadow in All Directions
  • general concept-java code and cycle clocks
  • Graph matplotlib to show total count in the histogram bins
  • Why won't my UITableViewCell deselect and update its text?
  • r - extract alphanumeric strings from text
  • eC (Ecere) how to not worry about private data fields of a class
  • Updating one element of a bound Observable collection
  • Greek letters in legend in R
  • Dynamically set properties from Dictionary in Swift
  • Need a consistent TimePicker gizmo for mobile web site
  • How to rotate the dial in a gauge chart? Using python plotly
  • How to show dropdown in excel using jrxml (jasper api)?