![](/img/trans.png)
[英]UITableView with custom UITableViewCell not displaying cell
[英]UITableview not updating uitableviewcell custom cell from storyboard
我尝试了尽可能多的解决方案来解决此问题。 我的最新代码很受打击:
static NSString *simpleTableIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
[[cell viewWithTag:1000] removeFromSuperview];
[[cell viewWithTag:2000] removeFromSuperview];
// other code for setting view that works perfect then
if(some condition){
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:@"upgrey.png"] forState:UIControlStateNormal];
btn.frame=upButton.frame;
[btn addTarget:self action:@selector(upButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:@"downgrey.png"] forState:UIControlStateNormal];
btn2.frame=downButton.frame;
[btn2 addTarget:self action:@selector(downButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn2];
btn.tag=1000;
btn2.tag=2000;
}
return cell;
但这不起作用。 如果我添加[[cell viewWithTag:1000] removeFromSuperview]; [[cell viewWithTag:2000] removeFromSuperview];
首先,从所有单元格中删除按钮。 如果我不使用这个。 它使用这两个按钮显示所有单元格。 提前致谢。
即使您已经接受了自己的答案,我也将花一点时间解释为什么会遇到这个问题,以及将来如何更好地构造UITableView
。
当您没有那些removeFromSubview
行时,之所以会“显示具有这两个按钮的所有单元格”,是因为通过实现dequeueReusableCellWithIdentifier:
您正在重用单元格及其内容。 在每次调用cellForRowAtIndexPath
过程中重复添加相同的子视图,使得您完全没有意义地重复使用单元格,因为您根本没有在单元格中重复使用任何内容。 因此,要么,不要再用你的细胞,在所有的,或者更好的,因为每个单元格的内容是完全一样的, 做你的重用细胞, 还重用这些按钮。
有两种好的方法可以做到这一点。 一种方法是创建UITableViewCell自定义子类。 但是由于单元格的内容非常简单,并且您说else
条件的功能已经起作用,所以我建议采用另一种方式,这种方式可能会减少代码密集性。 这是我的建议:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create two identifiers -- one for "some condition" one
// for the "other condition"
static NSString *simpleTableIdentifier1 = @"cell1";
static NSString *simpleTableIdentifier2 = @"cell2";
if (some condition) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier1];
// Only add the buttons when cell == nil, i.e. during the first
// time cell's initialized to hold a UITableViewCell
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier1]
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:@"upgrey.png"] forState:UIControlStateNormal];
btn.frame=upButton.frame;
[btn addTarget:self action:@selector(upButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:@"downgrey.png"] forState:UIControlStateNormal];
btn2.frame=downButton.frame;
[btn2 addTarget:self action:@selector(downButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn2];
}
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];
if (cell == nil) {
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.