Ios UITableView中的重用问题
我有UITableView,它覆盖了整个屏幕(480px) 每个电池的高度为300px 我一共有5行 下面是我使用的代码Ios UITableView中的重用问题,ios,objective-c,uitableview,reuseidentifier,Ios,Objective C,Uitableview,Reuseidentifier,我有UITableView,它覆盖了整个屏幕(480px) 每个电池的高度为300px 我一共有5行 下面是我使用的代码 -(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCel
-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
}
UIButton *myButton = (UIButton *)[cell viewWithTag:999999999];
int i = indexPath.row+1;
myButton.tag = i;
myButton.titleLabel.text = [NSString stringWithFormat:@"B - %d", i];
[myButton setTitle:[NSString stringWithFormat:@"B - %d", i] forState:UIControlStateNormal];
NSLog(@"tag set is %d & text for button is =====B-%d====", i,i);
[myButton addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (int) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
-(IBAction)btnSelected:(id)sender {
UIButton *button = (UIButton *)sender;
NSLog(@"my tag after click is ===%d", [button tag]);
}
现在,当我运行此代码时,我希望每个单元格都将写入B-1
到B-5
。然而在B-3
之后,我看到的只是B-1
和B-2
NSLog如下所示
2013-07-28 23:31:34.281 NewTest[1783:11303] tag set is 1 & text for button is =====B-1====
2013-07-28 23:31:34.284 NewTest[1783:11303] tag set is 2 & text for button is =====B-2====
2013-07-28 23:31:34.281 NewTest[1783:11303] tag set is 1 & text for button is =====B-1====
2013-07-28 23:31:34.284 NewTest[1783:11303] tag set is 2 & text for button is =====B-2====
2013-07-28 23:32:03.643 NewTest[1783:11303] tag set is 3 & text for button is =====B-3====
2013-07-28 23:32:03.719 NewTest[1783:11303] tag set is 4 & text for button is =====B-4====
2013-07-28 23:32:03.835 NewTest[1783:11303] tag set is 5 & text for button is =====B-5====
现在,当我完全向下滚动时,我得到如下所示的NSLog
2013-07-28 23:31:34.281 NewTest[1783:11303] tag set is 1 & text for button is =====B-1====
2013-07-28 23:31:34.284 NewTest[1783:11303] tag set is 2 & text for button is =====B-2====
2013-07-28 23:31:34.281 NewTest[1783:11303] tag set is 1 & text for button is =====B-1====
2013-07-28 23:31:34.284 NewTest[1783:11303] tag set is 2 & text for button is =====B-2====
2013-07-28 23:32:03.643 NewTest[1783:11303] tag set is 3 & text for button is =====B-3====
2013-07-28 23:32:03.719 NewTest[1783:11303] tag set is 4 & text for button is =====B-4====
2013-07-28 23:32:03.835 NewTest[1783:11303] tag set is 5 & text for button is =====B-5====
现在,当标签和文本设置正确时,为什么我看到最后两个按钮是B-1和B-2,而不是B-4和B-5
你知道怎么解决这个问题吗?
截图1
截图2
截图3
你知道如何解决这个问题吗?这样我就可以写出B-1到B-5了?
注意:如果我将单元格高度降低到100,我会看到所有按钮文本为B-1到B-5 这个问题是有关的,但这是一个更简单的版本。
我所做的是,不使用标签并使用accessibilityValue,我获取按钮单击的id。向下滚动时,表格视图将重用不再可见的单元格,因为它需要显示其他单元格。第一次发生这种情况的是电池“B-3”。由于表视图正在重用单元格,因此单元格中按钮的
标记先前已由代码设置为某个数字(可能为1)。因此,viewWithTag:99999999
将返回nil
。您的NSLog语句将使其看起来工作正常,但实际上您正试图在nil
按钮上设置标题,因此重用单元格中的按钮不会更新为正确的标题
解决方案:您可以使用以下属性创建UITableViewCell的自定义子类:
@property (nonatomic, weak) IBOutlet UIButton *button;
在故事板或xib中,将其连接到您的按钮。或者,如果需要,可以通过编程方式在单元格的awakeFromNib
或init方法中执行此操作。现在,您可以直接引用单元格。按钮
,而不必使用[cell view with tag:99999999]
ahhh。。。那么我该如何解决这个问题呢……)说真的,你至少让我知道出了什么问题。。。有很多关于自定义UITableViewCells的教程。