本文介绍了滚动时带有 UIButton 的 UITableViewCells 重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要创建一个带有一些按钮作为元素的表格视图,视图加载得很好,但是向下滚动按钮时会重叠.我认为旧按钮没有清除,新按钮放置在它上面.请帮我解决这个问题.提前致谢.
I need to create a table view with some buttons as its elements, view gets loaded fine, but while scrolling down buttons gets overlapped. I think old buttons are not cleared and new buttons are placing over it. Please help me to fix this problem. Thanks in advance.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath
{
NSLog(@"index path %ld",(long)indexPath.row);
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:[NSString stringWithFormat:@"data %ld",(long)indexPath.row] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 160.0, 25);
[cell.contentView addSubview:button];
return cell;
}
推荐答案
只需要换行
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
现在
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
(or) //use this in cell for rowatindexpath
for(UIView *view in cell.contentView.subviews){
if ([view isKindOfClass:[UIView class]]) {
[view removeFromSuperview];
}
}
这篇关于滚动时带有 UIButton 的 UITableViewCells 重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!