UITableView 禁用用户不可见的行
Posted
技术标签:
【中文标题】UITableView 禁用用户不可见的行【英文标题】:UITableView Disable the row where not visible to user 【发布时间】:2014-07-15 23:57:59 【问题描述】:我有一个自定义 UITableView(990x580 像素),表格的每个单元格内都有一个文本字段,文本字段由标签区分:
cell.myTextField.tag = indexPath.row;
假设我的表格中有 20 行,导致我看到表格的所有行都必须使用滚动条(因为表格只有 990x598 像素)
假设我们正在查看表格的第 20 行,并且第 1 行不再可见(除非我使用滚动向上),我想知道为什么当我尝试获取现有信息时在标签等于 1 的文本字段中,应用程序没有返回任何内容?
我意识到当这个文本字段对用户不可见时,它似乎已被禁用,想找到一种方法让它不翻转用户不可用的行,这是我下面的 Tableview:
- (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:
【问题讨论】:
【参考方案1】:UITableView
通过在单元格滚动出表格的可见部分时自动重用单元格来工作。这会在您再次配置重用单元格时重置单元格信息。
您需要将持久性信息存储在 viewController 级别(例如,在 NSArray
或 NSDictionary
中)。考虑使用 NSDictionary
并存储您的单元格内容 NSString
s 由单元格的 indexPath 索引。
还要注意以下几点
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,因为它会根据需要自动创建新单元格。
【讨论】:
以上是关于UITableView 禁用用户不可见的行的主要内容,如果未能解决你的问题,请参考以下文章
iOS / Swift 3.0:如何确定 UITableView 中当前可见的行?