2012-12-25 46 views
2

這剛剛完全踩住了我。UITableView和UILabel重複

我有一個自定義UIView和UILable,我已經添加到我的UITableView cell.contentView作爲子視圖。我有一個名爲arryData的數組,它包含大約100個字符串作爲數據,我想在我的表中顯示。問題是,當表創建時,我看到從我的arryData值的第一個0-4行,如果我不斷向下滾動表,我看到的是前五個字符串重複一遍又一遍。我究竟做錯了什麼?這是我的代碼。

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    //NSLog(@"Inside cellForRowAtIndexPath"); 

    static NSString *CellIdentifier = @"Cell"; 

    // Try to retrieve from the table view a now-unused cell with the given identifier. 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    UILabel *labelValue1 = [[UILabel alloc] initWithFrame:CGRectMake(70, 25, 200, 30)]; 

    // If no cell is available, create a new one using the given identifier. 
    if (cell == nil) 
    { 
     // Use the default cell style. 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     //cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

     UIImageView *imgVw1 = [[UIImageView alloc] initWithFrame:CGRectMake(11, 0, 300, 75)]; 
     imgVw1.image = [UIImage imageNamed:@"background_pic5.png"]; 
     imgVw1.userInteractionEnabled = YES; 
     imgVw1.exclusiveTouch = YES; 
     [cell.contentView addSubview:imgVw1]; 


     labelValue1.text = [arryData objectAtIndex:indexPath.row]; 
     labelValue1.font = [UIFont fontWithName:@"BradleyHandITCTT-Bold" size: 22.0]; 
     labelValue1.textColor = [UIColor whiteColor]; 
     labelValue1.textAlignment = UITextAlignmentCenter; 
     labelValue1.numberOfLines = 1; 
     labelValue1.backgroundColor = [UIColor clearColor]; 
     labelValue1.adjustsFontSizeToFitWidth = YES; 
     labelValue1.minimumFontSize = 10.0; 
     labelValue1.tag = (indexPath.row)+100; 
     [cell.contentView addSubview:labelValue1]; 

     NSLog(@"indexPath.row: %d - arrayData: %@..." , indexPath.row, [arryData objectAtIndex:indexPath.row]); 
    } 
    else 
    { 
     NSLog(@"indexPath.row: %d - arrayData: %@..." , indexPath.row, [arryData objectAtIndex:indexPath.row]); 

     labelValue1 = (UILabel *) [cell viewWithTag:((indexPath.row)+100)]; 
     labelValue1.text = [arryData objectAtIndex:indexPath.row]; 
    } 

    //Do this only if row has a value. For blank ones, Skip this 


    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
    { 
     //its iphone 
     cell.textLabel.font = [UIFont systemFontOfSize:13]; 
    } 


    if([arryDataMutable containsObject:indexPath]) 
    { 
     [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    } 
    else 
    { 
     [cell setAccessoryType:UITableViewCellAccessoryNone]; 
    } 

    //cell.backgroundColor = [UIColor whiteColor]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    return cell; 

} 

這是我的輸出看起來,當我向下滾動表像和所有我一直看到的是文字在我的labelValue1從indexPath.row 0 - 超過4重複和超過

indexPath.row: 0 - arrayData: Behind Whipped... 
indexPath.row: 1 - arrayData: Aftershock Whip... 
indexPath.row: 2 - arrayData: Bull Whipped Sound 1... 
indexPath.row: 3 - arrayData: Bull Whipped Sound 2... 
indexPath.row: 4 - arrayData: Bullet Fire Whip... 
indexPath.row: 5 - arrayData: Circus Whip... 
indexPath.row: 6 - arrayData: Circus Whip2... 

回答

4

更改這樣你在這種情況下,設置標籤,

labelValue1.tag = 100; 

,並把它讀作,

labelValue1 = (UILabel *) [cell viewWithTag:100]; 

由於您將這些單元出列,它將嘗試重用該單元,並在您爲單元分配內存時將標籤添加到該單元中。執行else部分時,您需要訪問已添加爲[cell viewWithTag:100];的相同標籤。

[cell viewWithTag:((indexPath.row)+100)];將返回爲零的第一次不可見的大多數單元格。既然你在重複使用單元格,你將會看到同樣的文本再次出現相同的前5個可見單元格。在這種情況下,您試圖將文本設置爲nil對象。所以它不會有任何區別,因此你會看到相同的文字重複。

+2

謝謝你的快速回答。即使我已經開發了很長時間的應用程序,這種整個桌面重用單元概念對我來說仍然是個謎。 –

+0

@SamBudda,很高興幫助。 :)是的,這需要一些時間才能理解。 – iDev