2009-08-25 101 views
6

我發現了一些類似於我的問題但不完全相同的帖子。在uitableviewcell上tableview中的重複行

在我的應用程序中,用戶可以在幾個可用視圖之間導航,以深入到所需的結果。當用戶前進時,然後向後,然後向前等,顯而易見的是行被重繪/重寫,並且文本變得更大膽和更大膽。

我發現在某些帖子中,這可能與我創建行的方式有關,使用cellforrowatindexpath方法中的uilable。

是否有我需要做的事情,以防止每次用戶在桌面視圖之間前後移動時重新填充/重繪行?我是否需要在下面的代碼中添加一些東西或者添加一些東西到viewwillappear方法中(目前在桌面上有一個'reloaddata',但似乎沒有幫助)?

這裏是我的代碼:

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

    UILabel *label = [[[UILabel alloc] init] autorelease]; 
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20]; 
    label.frame = CGRectMake(10.0f, 10.0f, 220.0f, 22.0f); 
    label.textColor = [UIColor blackColor]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.opaque = NO; 
    label.text = [mapareaArray objectAtIndex:indexPath.row]; 
    [cell.contentView addSubview:label]; 

    CustomCellBackgroundView *bgView = [[CustomCellBackgroundView alloc] initWithFrame:CGRectZero]; 
    bgView.borderColor = [UIColor clearColor]; 
    bgView.fillColor = [UIColor whiteColor]; 
    bgView.position = CustomCellBackgroundViewPositionSingle; 
    cell.backgroundView = bgView; 
    return cell; 
} 

回答

11

您所遇到的問題是由於該行:

[cell.contentView addSubview:label]; 

要添加子視圖表格單元格是否是一個新的小區或不。如果它是一箇舊的單元格(從可重用池中出列),那麼您將爲該單元格添加另一個子視圖。

相反,您應該標記UILabel,然後使用標記找到它以修改該UILabel的內容。添加(並設置其所有屬性),並標記的UILabel的,如果(細胞==零)塊中:

if(cell == nil) { 
    // alloc and init the cell view... 

    UILabel *label = [[[UILabel alloc] init] autorelease]; 
    label.tag = kMyTag; // define kMyTag in your header file using #define 
    // and other label customizations 
    [cell.contentView addSubview:label]; // addSubview here and only here 
    ... 
} 

然後找到它:

UILabel *label = (UILabel *)[cell.contentView viewWithTag: kMyTag]; 
label.text = [mapareaArray objectAtIndex:indexPath.row]; 

而無需重新添加它作爲if(cell == nil)塊之外的子視圖。子視圖已經在那裏了(這就是爲什麼重複使用單元格視圖效率更高,如果你做得正確的話,那就是;)。

+0

非常感謝你。大的幫助。 關於此問題的最後一個問題 - 我沒有編程背景,所以不知道在頭文件中使用#define是什麼意思。這是什麼語法?我以前看過它,並試圖在標題中放置'#define kMyTag',但這不起作用...我假設你需要將kMyTag定義爲一個值,但不確定這樣做的語法是什麼。你能幫我嗎? – SKayser 2009-08-25 11:49:24

1

.h文件中 的「定義」是在頭文件的頂部的#import聲明後說,並把爲0,因爲我不知道該怎麼去定義它:

#define kMyTag 0 

。 m文件 我已根據您的意見更新了此部分,但a)該表未填充,b)當用戶導航到下一個視圖並返回到此視圖時,它失敗並顯示「發送到實例的無法識別的選擇器」 ,和c)我不得不放入兩個'回單元';'條目或結束。我認爲我的東西順序錯誤,可能沒有正確初始化?

- (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.selectionStyle = UITableViewCellSelectionStyleNone; 

     UILabel *label = [[[UILabel alloc] init] autorelease]; 
     label.tag = kMyTag; // define kMyTag in your header file using #define 
     label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20]; 
     label.frame = CGRectMake(10.0f, 10.0f, 220.0f, 22.0f); 
     label.textColor = [UIColor blackColor]; 
     label.backgroundColor = [UIColor clearColor]; 
     label.opaque = NO; 

     CustomCellBackgroundView *bgView = [[CustomCellBackgroundView alloc] initWithFrame:CGRectZero]; 
     bgView.borderColor = [UIColor clearColor]; 
     bgView.fillColor = [UIColor whiteColor]; 
     bgView.position = CustomCellBackgroundViewPositionSingle; 
     cell.backgroundView = bgView; 

     [cell.contentView addSubview:label]; // addSubview here and only here 
     return cell; 
    } 
UILabel *label = (UILabel *)[cell.contentView viewWithTag: kMyTag]; 
label.text = [mapareaArray objectAtIndex:indexPath.row]; 
return cell; 
} 
+0

我不確定您是否應該使用0作爲kMyTag的值。嘗試從1開始,因爲0可能是所有事情的默認值。在更改之後,刪除if塊內的返回單元。 – tchen 2009-08-25 19:32:55

+0

再次感謝!現在可行!感謝你的時間! – SKayser 2009-08-26 08:17:04

0

您應該創建的UITableViewCell子類爲特定的細胞,然後應用邏輯來看看你是否需要每次子視圖添加到自身。此外,使用UITableviewcell的prepareForReuse並在此期間刪除任何子視圖,然後再應用您想將單元添加到UILabel的邏輯。