我有一个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/