2010-09-11 43 views
1

我有一個UITableView從一個NSMutable數組中有79個條目填充。當我運行我的應用程序並向下滾動表格時,似乎在一個單元格中有多個條目。它似乎發生在屏幕高度和桌子下半部分。cellLabel.text的內容似乎覆蓋隨機單元格本身

例如:在陣列中

一個目的是@「狗」,另一個是@「貓」。在一個單元格中,您可以看到在單元格標籤文本中寫入的貓和狗。

代碼init'ing細胞

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    // Configure the cell... 

    UILabel *cellLabel = [[[UILabel alloc] initWithFrame:cell.frame] autorelease]; 
    cellLabel.text = [symptomsArray objectAtIndex:indexPath.row]; 
    cellLabel.textColor = [UIColor blackColor]; 
    cellLabel.backgroundColor = [UIColor clearColor]; 
    cellLabel.opaque = NO; 
    [cell.contentView addSubview:cellLabel]; 
    cell.contentView.backgroundColor = [UIColor colorWithRed:(214.0/255) green:(215.0/255.0) blue:(217.0/255) alpha:1.0]; 

    return cell; 
} 

回答

1

保持簡單。

如果只有幾個條目,爲什麼不加載之前分配單元陣列和陣列直接在裝載單元對象:

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

return [arrayOfCells objectAtIndex:indexPath.row]; //already allocated one single time at the beginning 

    } 

沒有更多dequeueReusableCellWithIdentifier ..

保持它簡單!

+0

謝謝。這最終成爲我最好的方式。 – Pick 2010-09-22 23:40:22

1

那是因爲你要添加標籤每次 如果你真的需要一個新的UILabel那麼,如果你有和改變它的句子文本中添加出來的中頻

並有效地做到這一點你必須繼承的UITableViewCell也許

希望這有助於

1

子視圖相對於其超級視圖的界限進行定位。因此,例如,如果您想要cellLabel位於內容視圖的左上角,則需要將其原點設置爲(0,0)。您正在使用單元格的框架,該框架相對於表格視圖的左上角。

向下滾動時,UITableView正在重複使用舊單元格,並且正在向這些單元格添加其他標籤。這些單元格的起始位置由表格視圖頂部的一個或兩個屏幕偏移,因此您將新標籤放置在單元格的實際邊界之外。以下是如何解決它。

static int CellLabelTag = 12345; 

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    cell.accessoryType = UITableViewCellAccessoryTypeNone; 

    UILabel *cellLabel = [[[UILabel alloc] initWithFrame:cell.contentView.bounds] autorelease]; 
    cellLabel.tag = 585493; 
    [cell.contentView addSubview:CellLabelTag]; 
} 

UILabel *cellLabel = (UILabel *)[cell viewWithTag:CellLabelTag]; 
cellLabel.text = // your text here 
// blah blah blah configuration 
return cell; 
1
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; 
if (cell == nil) { 
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; 
} 

確保 「dequeueReusableCellWithIdentifier」 和 「reuseIdentifier」 應爲 「零」

現在,它的工作!

+0

你是否在暗示reuseIdentifier:nil是最佳實踐? – 2014-07-29 20:31:12

0

亞歷克斯答案是最好的一個,很多人都沒有意識到這種情況正在發生,特別是如果他們沒有使用ClearColor背景標籤,在這種情況下,你永遠不會看到這種覆蓋。

我的首選解決方案是創建一個具有所需屬性的自定義單元格。因此,在這種情況下,您將創建

@interfacce MyCustomCell : UITableViewCell 
    @property (nonatomic) UILabel *cellLabel; 
@end 

給它一個屬性的UILabel * cellLabel,並盡一切你有上面除了將代碼從設置在MyCustomCell的初始化標籤的文本。米,自更換電池的任何實例,例如:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 

    if (self) 
    { 
     self.cellLabel = [[UILabel alloc] initWithFrame:cell.frame]; 
     self.cellLabel.textColor = [UIColor blackColor]; 
     self.cellLabel.backgroundColor = [UIColor clearColor]; 
     self.cellLabel.opaque = NO; 
     [self..contentView addSubview:cellLabel]; 
     self.contentView.backgroundColor = [UIColor colorWithRed:(214.0/255) green:(215.0/255.0) blue:(217.0/255) alpha:1.0]; 
    } 

    return self; 
} 

現在,在您的cellForRowAtIndexPath使用MyCustomCell,在那裏你檢查是否細胞==零,你可能還需要檢查電池標籤:

if(cell == nil || cell.cellLabel == nil) 

初始化它以完全相同的方式:現在

cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

,所有你需要做的是設置:

cell.cellLabel.text = [symptomsArray objectAtIndex:indexPath.row]; 

您在cellForRowAtIndexPath中的代碼更清潔,內存效率更高,並且您不會得到您的錯誤。

請記住在界面構建器中將您的單元設置爲MyCustomCell類型。