我正在使用 UITableView,我注意到我的表格视图中的单元格在我滚动时变得越来越粗,它正在覆盖内容,我想停止这个但看不到我哪里出错了。
在我的 UITableView 上,由于某种原因,当我滚动 tableview 的内容时,手动创建的 UILabel 会变得混乱。
我需要一个手动的 UILabel,因为我以后需要自定义单元格。
当我上下滚动时,标签会越来越粗。它们总是重叠,有时甚至会影响较低的行(甚至在它们位于视口中之前)。
如果我继续这样做,单元格内容就会变得难以理解。
仅当 backgroundColor 未设置为 时才会发生这种情况clearColor
。
我试过了[cellLabel setClearsContextBeforeDrawing:YES];
,[self.tableView setClearsContextBeforeDrawing:YES];
没有效果。
如果我使用cell.textLabel.text
,那么问题似乎就消失了。
代码和图像示例如下。
// Simple table view
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
//[self configureCell:cell atIndexPath:indexPath];
NSString *txt = @"Product";
//cell.textLabel.text = txt;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *cellView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, cell.frame.size.height)];
UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 120, 35)];
[cellLabel setText:txt];
[cellLabel setFont:[UIFont boldSystemFontOfSize:12]];
[cellLabel setBackgroundColor:[UIColor clearColor]];
[cellView addSubview:cellLabel];
[cellLabel release];
[cell.contentView addSubview:cellView];
[cellView release];
return cell;
}
Image follows;
![image of uitableview][1]
[1]: http://i.stack.imgur.com/5lNy6.png
// Edit to include context
I am using a dictionary to display the contents of the UITableViewCells.
I have attempted to do the following;
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[self configureCell:cell atIndexPath:indexPath];
} // end if
// Configure the cell...
//
// Moved to inside the cell==nil
return cell;
}
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
// Get the txt from the Dictionary/Plist... *removed due verboseness*
UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 120, 35)];
[cellLabel setText:txt];
[cellLabel setFont:[UIFont boldSystemFontOfSize:12]];
[cellLabel setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:cellLabel];
[cellLabel release];
}
这虽然解决了覆盖问题——它导致了一个问题——它使标签重复出现在完全随机的地方——以下只是一个示例,其他字段和标签也会重复。
见下图;