gpt4 book ai didi

ios - 如何阻止 UITableView 单元格覆盖内容?

转载 作者:技术小花猫 更新时间:2023-10-29 10:42:24 26 4
gpt4 key购买 nike

我正在使用 UITableView,我注意到我的表格 View 中的单元格在我滚动时变得越来越粗,它正在覆盖内容,我想停止它但看不到我哪里出错了。

在我的 UITableView 上,出于某种原因,当我滚动 tableview 的内容时,手动创建的 UILabel 弄乱了。

我需要一个手动 UILabel,因为稍后我需要自定义单元格。

当我上下滚动时,标签逐渐变得越来越粗;它们总是重叠,有时甚至会影响下方的行(甚至在它们进入视口(viewport)之前)。

如果我继续这样做,单元格内容会变得难以理解。

只有当 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];
}

这个,虽然它解决了覆盖的问题——它会导致一个问题——它让标签重复出现在完全随机的地方——下面只是一个例子,其他字段和标签也会重复。

见下图;

repeating labels in uitableview

最佳答案

    // cell reuse
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

返回给你的单元格已经被使用过,它已经有一个 UILabel subview ,你正在添加另一个。将添加的 subview 放在该部分

   if (cell == nil) { //cell initialization

并根据需要编辑 subview 单元格初始化后,您可以通过标签访问它们。

关于ios - 如何阻止 UITableView 单元格覆盖内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9794750/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com