![](/img/trans.png)
[英]How to load Images from NSMutableArray in UITableviewCell in iPhone?
[英]iPhone - NSMutableArray - UITableViewCell
你好伙计
我有一个表视图,我从JSON输出加载。 我已经实现了检查和取消选中UITableView单元格的功能。
问题1 - 当我滚动到视图的底部时,顶部的所有选定单元格都将被取消选中。 我不知道为什么会发生这种情况,如果有人可以指导我:)
问题2 - 我想抓取tableview中的所有选定单元格,并将它们放入NSMutableArray中。 我知道有很多方法可以做到这一点,但你们可以告诉我一个更有效的方法。 我花了将近2天的时间来搞清楚这一点,但仍然没有运气!
谢谢和最诚挚的问候
好吧,这是讨厌的,因为你可能还没有完全掌握UITableView的工作原理。
它重用了单元格,这就是你在UITableView
上调用dequeTableCellWithIdentifier:
方法的原因。 这意味着它将重用其中一个不再在视图中的单元格。
不要为UITableViewCell
中的每个项添加UITableViewDataSource
,这可能/将占用大量内存,并且您的UITabelView
会感觉非常慢。
只需确保数据源包含对象所需的所有数据,而不是UITableViewCell
。 含义标题,选定状态,......
你需要做的是设置在单元的选择状态UITableViewDataSource
项目,并期待选定在规定UITableViewDataSource
,而不是UITableViewCell
。
因此,在您从JSON检索的对象中添加一个选择的属性,该属性采用BOOL。
然后在UITableViewDataSoutrce
方法中, cellForRowAtIndexPath
执行以下操作:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"SomeCellIdentiefier";
UITableViewCell *cell = (SongTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
//Load new cell
}
YouJSONObject *item = [self.array objectAtIndex:indexPath.row];
if (item.selecte) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
然后你的UITableViewDelegate
方法didSelectRowAtIndexPath
做类似的事情:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
YouJSONObject *item = [self.array objectAtIndex:indexPath.row];
item.selected = !item.selected;
if (item.selecte) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.