我需要创建一个表格视图,其中一些按钮作为其元素,视图可以很好地加载,但是向下滚动按钮会重叠。我认为旧的按钮没有被清除,新的按钮放在它上面。请帮我解决这个问题。提前谢谢。
- (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;
}
答案 0 :(得分:3)
只需替换
行 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];
}
}
答案 1 :(得分:1)
- (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;
}
答案 2 :(得分:1)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"index path %ld",(long)indexPath.row);
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
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;
}
答案 3 :(得分:1)
我建议使用标签。请参阅下面的代码。
- (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];
}
NSInteger buttonTag = 100500;
UIButton *button = [cell.contentView viewWithTag: buttonTag]
if(button == nil)
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 160.0, 25);
[cell.contentView addSubview:button];
}
[button setTitle:[NSString stringWithFormat:@"data %ld",(long)indexPath.row] forState:UIControlStateNormal];
return cell;
}
答案 4 :(得分:1)
如果您使用Xib,则可以使用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell==nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
答案 5 :(得分:0)
请在添加按钮作为子视图之前插入以下代码,它会删除单元格内容视图中的所有按钮,然后再添加新按钮。
for(UIView *view in cell.contentView.subviews){
if ([view isKindOfClass:[UIButton class]]) {
[view removeFromSuperview];
}
}