tableView reloadData无法正常工作:未调用cellForRowAtIndexPath

时间:2011-09-23 11:51:01

标签: iphone uitableview

我遇到此问题cellForRowAtIndexPath未调用reloadingData。我花了很多时间在这上面。我xib文件dataSourcedelegate已连接到File's Owner。任何想法都是受欢迎的。

这是我的代码:

* 的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方法 * ***

- (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中 ** *

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

4 个答案:

答案 0 :(得分:2)

我在你的代码中发现了一些弱点:

  1. 您应该从cell.accessoryView

  2. 中设置if (cell == nil){}
  3. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath中,您可以使用tableView而不使用tableViewDistance(可能是nil?)

  4. - (void)viewDidLoad中,您应先调用[super viewDidLoad];,然后进行自定义。

  5. 希望对你有所帮助。 GL

答案 1 :(得分:1)

将tableView插座(UITableView * tableViewDistance)连接到您的Xib TableView。

答案 2 :(得分:0)

尝试致电

[tableView reloadData];

而不是

 [self.tableViewDistance  reloadData];

答案 3 :(得分:0)

根据您对问题的评论,您希望一次只选择一个单元格,因此您要取消选择之前选择的单元格并选择当前选定的单元格。您可以使用-indexPathForSelectedRow的{​​{1}}方法查找所选行:

UITableView

您还需要实施- (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 = ...; }

tableView:didDeselectRowAtIndexPath: