iphone - cellLabel.text的内容似乎在随机单元格中覆盖了自身

标签 iphone objective-c uitableview ios

我有一个UITableView是从NSMutable数组中填充的,该数组中有79个条目。当我运行我的应用程序并向下滚动表时,似乎在一个单元格中有多个条目。好像是屏幕高度下降了一半。

例如:

数组中的一个对象是@“Dog”,另一个是@“Cat”。在一个单元格中,您可以在cellLabel文本中看到猫和狗。

初始化单元格的代码

// 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;
}

最佳答案

把事情简单化。

如果只有几个条目,为什么不先分配单元格数组,然后再直接从内部数组中加载单元格对象:

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

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

    }

不再有dequeueReusableCellWithIdentifier ..

把事情简单化!

关于iphone - cellLabel.text的内容似乎在随机单元格中覆盖了自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3689280/

相关文章:

iphone - 对象 C : Creating a standard try-catch-block for use through the application

iOS - NSMutableArray 不会被填充

ios - 如何更改 UITableViewRowAction 标题颜色?

ios - UITableViewCell subview 不接收触摸

ios - UITableView 中的 NSDictionary - 使用索引路径

iphone - 在 iPAD 中设置下拉菜单

iphone - UIPageViewController延迟换页动画

ios - 用于 Unwind Segue 的自定义 Segue 类

ios - Xcode 6 swift multiple segues with prepare for segue

objective-c - mapViewDidFinishLoadingMap : called too early