简体   繁体   English

tableView reloadData不起作用:未调用cellForRowAtIndexPath

[英]tableView reloadData not working: cellForRowAtIndexPath not called

I have this problem cellForRowAtIndexPath is not called by reloadingData . 我有这个问题cellForRowAtIndexPath不被reloadingData调用。 I have spent a lot of time with this. 我花了很多时间。 I xib file dataSource and delegate are connected to File's Owner . 我xib文件dataSourcedelegate连接到File's Owner Any idea is welcome. 欢迎任何想法。

This is my code : 这是我的代码:

* cellForRowAtIndexPath * * cellForRowAtIndexPath *

- (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;
}

didSelectRowAtIndexPath * *** didSelectRowAtIndexPath * ***

- (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];
}

* viewDidLoad ** * * viewDidLoad ** *

- (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.
}

I've found some weak parts in your code: 我在您的代码中发现了一些弱点:

  1. You should set cell.accessoryView out of if (cell == nil){} 您应该将cell.accessoryView设置为if (cell == nil){}

  2. In - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath you can use tableView and not use tableViewDistance (may be it is nil ?) - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath您可以使用tableView而不使用tableViewDistance (可能是nil吗?)

  3. In - (void)viewDidLoad you should call first [super viewDidLoad]; - (void)viewDidLoad您应该先调用[super viewDidLoad]; and then make customizations. 然后进行自定义。

Hope that will help you. 希望能帮到你。 Gl GL

将您的tableView出口(UITableView * tableViewDistance)连接到Xib TableView。

Try to call 尝试致电

[tableView reloadData];

instead of 代替

 [self.tableViewDistance  reloadData];

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: 您可以使用UITableView-indexPathForSelectedRow方法查找选定的行:

- (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: : 您还将需要实现tableView:didDeselectRowAtIndexPath: ::

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM