2011-09-23 71 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。

相關問題