2011-09-23 72 views
1

我有这个问题cellForRowAtIndexPath不是由reloadingData调用。我花了很多时间。我将xib文件dataSourcedelegate连接到File's Owner。任何想法是受欢迎的。的tableView reloadData不工作:的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方法* ***

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

你是什么意思,它不reloadData,什么时候?既然你在viewDidLoad中设置了所有的设置,无论如何这只会被调用一次(通常)。只有ViewDidAppear或viewWillAppear被多次调用。你有修改阵列吗?你能澄清一下吗? – user387184

+0

我需要重新加载tableview后,我选择了一排,因为我需要一次只有一个选择行 – Gabrielle

回答

2

我发现在你的代码的一些薄弱环节:

  1. 您应该设置cell.accessoryViewif (cell == nil){}

  2. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath你可以使用tableView,并且不使用tableViewDistance(可能是它是nil?)

  3. - (void)viewDidLoad,您应该先拨打[super viewDidLoad];,然后进行自定义。

希望能帮到你。 GL

+0

非常感谢Nekto ..问题是cell.accessoryView if(cell == nil){} – Gabrielle

+0

欢迎您。 – Nekto

0

尝试调用

[tableView reloadData]; 

,而不是

[self.tableViewDistance reloadData]; 
+0

我试过它self.tabelViewDistance之前,但它是相同的 – Gabrielle

0

基于对这个问题的评论,你想有只一次一个选定的单元格,所以要取消先前选定的单元格,选择当前选择的一个。您可以使用-indexPathForSelectedRow方法UITableView找到所选行:

你也将要实现tableView:didDeselectRowAtIndexPath:

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

您的tableView出口(UITableView的* tableViewDistance)连接到您的厦门国际银行的TableView。

相关问题