我有一个自定义的 UITableView(990x580 像素),其中表格的每个单元格内都有一个文本字段,文本字段由标签区分:
cell.myTextField.tag = indexPath.row;
假设我的表格中有 20 行,导致我看到表格的所有行将不得不使用滚动(因为表格只有 990x598 像素)
假设我们正在查找表格的第 20 行,并且第 1 行不再可见(除非我使用滚动向上),我想知道为什么当我尝试获取现有信息时在标签等于 1 的文本字段中,应用程序没有返回任何内容?
我意识到,当这个文本字段对用户不可见时,它似乎被禁用了,想找到一种方法让它不翻转用户不可用的行,这是我的表格 View 如下:
- (UITableViewCell *)tableView:(UITableView *)tableView2 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//Ja explicado anteriormente
static NSString *simpleTableIdentifier = @"ComissoesTableViewCell";//Ja explicado anteriormente
ComissoesTableViewCell *cell = (ComissoesTableViewCell *)[tableView2 dequeueReusableCellWithIdentifier:simpleTableIdentifier];//Ja explicado anteriormente
if (cell == nil){//Ja explicado anteriormente
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ComissoesTableViewCell" owner:self options:nil];//Ja explicado anteriormente
cell = [nib objectAtIndex:0];//Ja explicado anteriormente
}
cell.data.tag = indexPath.row;
return cell:
}
最佳答案
UITableView
的工作原理是在单元格滚动出表格的可见部分时自动重复使用这些单元格。这使得单元格信息在您再次配置重用单元格时重置。
您需要在 viewController 级别存储持久化信息(例如,在 NSArray
或 NSDictionary
中)。考虑使用 NSDictionary
并存储由单元格的 indexPath 索引的单元格内容 NSString
。
还要注意以下几点
if (cell == nil){//Ja explicado anteriormente
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ComissoesTableViewCell" owner:self options:nil];//Ja explicado anteriormente
cell = [nib objectAtIndex:0];//Ja explicado anteriormente
}
不再推荐。
如果您为 simpleTableIdentifier
注册了一个 nib 或类(例如,使用 - (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
)保证 deque 方法永远不会返回 nil,因为它会根据需要自动创建新的单元格。
关于ios - UITableView 禁用用户不可见的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24770070/