【发布时间】:2014-05-16 18:36:32
【问题描述】:
我需要创建一个表格视图,其中包含一些按钮作为其元素,视图可以正常加载,但是向下滚动时按钮会重叠。我认为旧按钮没有被清除,新按钮被放置在它上面。请帮我解决这个问题。提前致谢。
- (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;
}
【问题讨论】:
-
可以删除 if(cell==nil) 代码块,dequeueReusableCellWithIdentifier 已经够用了。
标签: ios objective-c uitableview uibutton