gpt4 book ai didi

ios - 如何删除 uitableviewcell 中重复的按钮?

转载 作者:行者123 更新时间:2023-11-28 21:56:41 24 4
gpt4 key购买 nike

我是新手。我创建了一个 UITableView 并使用下面给出的代码在特定行(例如,第二行)中添加了一个 UIButton。当我滚动表格 View 时,我在表格中得到了一个按钮的副本。

提前致谢,请给我一个解决方案。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [items objectAtIndex:[indexPath row]];

CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

CGFloat height = MAX(size.height, 44.0f);

if (indexPath.row ==1) {
return height + (CELL_CONTENT_MARGIN * 2) + CELL_HEIGHT;
}
else{
return height + (CELL_CONTENT_MARGIN * 2);
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];

label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];


// [[label layer] setBorderWidth:2.0f];





[[cell contentView] addSubview:label];
}


if (indexPath.row==1) {

UIButton *Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//Button.frame = CGRectMake(120, 120, 80, 24);
NSString *text =@"BUTTON" ;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];


Button.frame = CGRectMake(65, 150, 160, 24);

[Button addTarget:self
action:@selector(load)
forControlEvents:UIControlEventTouchUpInside];
Button.backgroundColor = [UIColor redColor];
Button.layer.cornerRadius = 5;
[Button setTitle:text forState:UIControlStateNormal];
[cell addSubview:Button];
}
NSString *text = [items objectAtIndex:[indexPath row]];

CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

if (!label)
label = (UILabel*)[cell viewWithTag:1];

[label setText:text];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];

return cell;

}

最佳答案

您获得多个按钮的原因是因为您每次调用 cellForRowAtIndexPath 时都会将按钮实例添加到单元格中。此调用经常发生并且单元格被重用(出队)。

处理自定义单元格的正确方法是对单元格进行子类化,然后在请求正确的索引时设置适当的单元格:

if (index == 1) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"**Cell**"];
else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"**OtherCell**"];
}

关于ios - 如何删除 uitableviewcell 中重复的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26311520/

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