2011-08-03 65 views
1

所以我有一些問題與我的tableview。我有一個自定義的label,我將其放入tableview cell中以添加比標準UItableviewcell更好的圖形。不過,我跑了我的第一個問題,UITableview dequeueReusableCellWithIdentifier和滾動冷凍問題

  1. ,我不得不對cells的文本標籤與正在發生變化,並在滾動在寫對方,只有當cells已經移出屏幕,然後回來。經過一些研究,我發現它可能與dequeueReusableCellWithIdentifier:有關,所以我調整了我的代碼。這是問題2進來的地方。

  2. 當我加載table一切都在它的正確位置,正確的期待和所有。但是,當我開始scroll下來時,除了最後一個,我可以到達所有的細胞,它將進入第8個細胞的底部並凍結,但我應該加載9個細胞。

我很困惑,有人能提供一些代碼或指導來幫助我嗎?

謝謝。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 


NSLog(@"Run"); 
CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
static NSString *CellIdentifier = @"Cell"; 
UILabel *label; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
NSArray *keys = [[appDelegate rowersDataStore] allKeys]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    // Configure the cell... 


    label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 15, cell.bounds.size.width - 10, 30)] autorelease]; 
    label.font = [UIFont boldSystemFontOfSize:16]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.shadowColor = [UIColor colorWithWhite:1.0 alpha:0.5]; 
    label.shadowOffset = CGSizeMake(0,1); 
    label.textColor = [UIColor colorWithRed:0x4c/255.0 green:0x4e/255.0 blue:0x48/255.0 alpha:1.0]; 

    switch (indexPath.section) { 
     case 0: 
      label.frame = CGRectMake(0, 15, cell.bounds.size.width - 10, 30); 
      label.textAlignment = UITextAlignmentCenter; 
      break; 
     case 1: 

      label.textAlignment = UITextAlignmentLeft; 
      UIImage *accessoryImage = [UIImage imageNamed:@"content_arrow.png"]; 
      UIImageView *accessoryView = [[UIImageView alloc] initWithImage:accessoryImage]; 
      cell.accessoryView = accessoryView; 
      [accessoryView release]; 
      break; 
    } 



    UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.frame]; 
    UIImage* img = [UIImage imageNamed:@"odd_slice.png"]; 
    imgView.image = img; 
    cell.backgroundView = imgView; 
    [imgView release]; 

    //Selected State 
    UIImage *selectionBackground = [UIImage imageNamed:@"row_selected.png"]; 
    UIImageView *selectionView = [[UIImageView alloc] initWithFrame:cell.frame]; 
    selectionView.image = selectionBackground; 
    cell.selectedBackgroundView = selectionView; 
    [selectionView release]; 
} 

switch (indexPath.section) { 
    case 0: 
     [label setText:@"Click to add new rower"]; 
     break; 
    case 1: 
     [label setText:[[[appDelegate rowersDataStore] objectForKey:[keys objectAtIndex:indexPath.row]] objectForKey:@"Name"]]; 
     break; 
} 

//Adds Text 
[cell addSubview:label]; 

return cell; 

}

+0

請你們看看這個問題,以及... [鏈接](http://stackoverflow.com/q/10276165/ 1230155) – madLokesh

回答

4

我在這裏看到的幾個問題。首先,該方法的總體結構應該是...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    // Attempt to dequeue the cell 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    // If cell does not exist, create it, otherwise customize existing cell for this row 
    if (cell == nil) { 
     // Create cell 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     // Configure cell: 
     // *** This section should configure the cell to a state independent of 
     // whatever row or section the cell is in, since it is only executed 
     // once when the cell is first created. 
    } 

    // Customize cell: 
    // *** This section should customize the cell depending on what row or section 
    // is passed in indexPath, since this is executed every time this delegate method 
    // is called. 

    return cell; 
} 

基本上,UITableView使用單個UITableViewCell實例繪製的每一個細胞中的表視圖。所以,當你第一次創建這個單元格時,你應該將它配置到一個對於所有使用這個實例的單元格通用的狀態,而不管在indexPath中傳遞的任何行或段是什麼。在你的例子中,這包括創建標籤,圖像和背景圖像實例,並將它們作爲子視圖添加到單元格中。

一旦單元格被創建(又名if (cell == nil)語句之外),您應該根據單元格應該如何查找包含在indexPath中的特定行和部分來定製其屬性。由於您想在代碼的這一部分訪問您的自定義標籤,因此我爲其分配了一個tag的值,以便我們可以在使用viewWithTag:創建它的代碼段之外訪問它。一旦我們有了標籤,我們就可以根據該部分對其進行自定義,並做我們想要的任何事情,例如自定義附件視圖。

我稍微修改/清理了下面的代碼。這是迄今爲止最不高效或優雅的方式來做你想做的事情,但我試圖保持儘可能多的代碼。我沒有測試過這一點,但如果你想這樣它應該工作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"Run"); 
    CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    NSArray *keys = [[appDelegate rowersDataStore] allKeys]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     // Configure the cell... 

     UILabel *label; 
     label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 15, cell.bounds.size.width - 10, 30)] autorelease]; 
     label.font = [UIFont boldSystemFontOfSize:16]; 
     label.opaque = NO; 
     label.backgroundColor = [UIColor clearColor]; 
     label.shadowColor = [UIColor colorWithWhite:1.0 alpha:0.5]; 
     label.shadowOffset = CGSizeMake(0,1); 
     label.textColor = [UIColor colorWithRed:0x4c/255.0 green:0x4e/255.0 blue:0x48/255.0 alpha:1.0]; 
     label.tag = 100; 
     [cell addSubview:label]; 
     [label release]; 

     UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.frame]; 
     UIImage* img = [UIImage imageNamed:@"odd_slice.png"]; 
     imgView.image = img; 
     cell.backgroundView = imgView; 
     [imgView release]; 

     //Selected State 
     UIImage *selectionBackground = [UIImage imageNamed:@"row_selected.png"]; 
     UIImageView *selectionView = [[UIImageView alloc] initWithFrame:cell.frame]; 
     selectionView.image = selectionBackground; 
     cell.selectedBackgroundView = selectionView; 
     [selectionView release]; 
    } 

    UILabel *lbl = (UILabel *)[cell viewWithTag:100]; 
    switch (indexPath.section) { 
     case 0: 
      cell.accessoryView = nil; 

      lbl.frame = CGRectMake(0, 15, cell.bounds.size.width - 10, 30); 
      lbl.textAlignment = UITextAlignmentCenter; 
      [label setText:@"Click to add new rower"]; 
      break; 
     case 1: 
      UIImage *accessoryImage = [UIImage imageNamed:@"content_arrow.png"]; 
      UIImageView *accessoryView = [[UIImageView alloc] initWithImage:accessoryImage]; 
      cell.accessoryView = accessoryView; 
      [accessoryView release]; 

      lbl.frame = CGRectMake(20, 15, cell.bounds.size.width - 10, 30); 
      lbl.textAlignment = UITextAlignmentLeft; 
      [lbl setText:[[[appDelegate rowersDataStore] objectForKey:[keys objectAtIndex:indexPath.row]] objectForKey:@"Name"]]; 
      break; 
    } 

    return cell; 
} 
+0

嗯,似乎沒有工作,加載tableview,但沒有文本,並退出時,我與它交互。你會說正確地做到這一點的最好方法是創建一個自定義的UITableviewcell類,然後將其添加到tableview中? –

+0

作了一些編輯。當您希望背景顏色清晰時,標籤應設置爲不透明,以便可能對文本做了某些操作。至於退出交互,我不得不看到與之相關的代碼,就像在你的'tableView:didSelectRowAtIndexPath:'委託方法中一樣。此方法僅繪製單元格,與用戶交互無關。 – Matt

+0

調試提示:因爲你在這裏做了很多事情,比如在一個單元格中添加一些子視圖,請嘗試逐個註釋獨立的部分,然後再次運行該應用程序以嘗試隔離代碼塊發生的位置。也使用斷點。對於我或任何試圖幫助您知道問題出在哪裏的人來說,這很有用。 – Matt