Ios UITableView:在顶部添加单元格时是否保留选择(单选)?
此代码将在使用reloadData刷新UITableView后恢复单元格选择:Ios UITableView:在顶部添加单元格时是否保留选择(单选)?,ios,objective-c,uitableview,selection,reloaddata,Ios,Objective C,Uitableview,Selection,Reloaddata,此代码将在使用reloadData刷新UITableView后恢复单元格选择: NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow]; [self.tableView reloadData]; [self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositio
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
[self.tableView reloadData];
[self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
当然,当我在TableView的顶部添加新单元格时,这个解决方案不起作用。在顶部添加单元格时,如何保持选择?这取决于您的数据源。最常见的简单情况示例是,如果是数组,则每次选择一行时都可以“记住”该对象(
tableView:didSelectRowAtIndexPath:
)
,并在[表重新加载数据]之后代码>,选择表索引:
NSUInteger index = [myArray indexOfObject:myObject];
if (index != NSNotFound) {
NSINdexPath *selectedIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
[self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
但是,正如我所说的,这实际上取决于很多因素,这只是基本设置的一个示例,其中表格填充了来自myArray
的数据,而没有重新排列和省略项目(并且只有一个部分).您可以简单地跟踪添加到顶部的单元格数量,并在此基础上使用selectrowatinexpath:
增加所选行的索引
或者,如果您有任何类型的UI元素(UIButton、UILabel等),那么您可以在didSelectRowAtIndexPath:
中设置该元素的标记,然后使用类似于UITableViewCell*selectCell=(UITableViewCell*)[[self.view viewWithTag:100]的方法访问具有该标记的元素的单元格(重新加载后)超级视图]代码>尝试以下操作:
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
//end of loading
int anyIndex = <any number of cell>
NSIndexPath *anyIndexPath = [[NSIndexPath alloc] indexPathForRow:anyIndex inSection:0];
UITableViewCell *anyCell = [table cellForRowAtIndexPath: firstIndexPath]
anyCell.selectionStyle = UITableViewCellSelectionStyle.Default
}
}
-(void)tableView:(UITableView*)tableView将显示单元格:(UITableViewCell*)用于rowatindexpath的单元格:(nsindepath*)indepath
{
if([indexPath行]==((NSIndexPath*)[[tableView indexPathsForVisibleRows]lastObject])。行){
//装载结束
int anyIndex=
nsindepath*anyindepath=[[nsindepath alloc]indexPathForRow:anyIndex片段:0];
UITableViewCell*anyCell=[表CellForRowatineXpath:firstIndexPath]
anyCell.selectionStyle=UITableViewCellSelectionStyle.Default
}
}
这应该是可行的。正如@Frane Poljak所说,用户点击tableView表示该行中包含的对象是用户感兴趣的对象。必须将对象保留为引用,才能再次选择包含该对象的单元格
假设您有一个对象的数组
,您可以这样做
enum Value
{
case NotSelected
case Selected(object)
}
private var selectionState : Value = .NotSelected
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectionState = Selected(dataSource[indexPath.row])
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
[...]
switch selectionState {
case .Selected(let object as! Object):
if dataSource[indexPath.row] == object { cell.selected = true }
else { cell.selected = false }
default : cell.selected = false
}
}
除了将第一个单元格的选择样式设置为UITableViewCellSelectionStyleDefault
,这还能做什么?您想要什么?始终是要选择的第一个项目?如果您阅读OP,问题将询问如何在重新加载UITableView后保留对特定单元格的选择。不一定是第一个手机…更新了我的答案,请看一看。
enum Value
{
case NotSelected
case Selected(object)
}
private var selectionState : Value = .NotSelected
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectionState = Selected(dataSource[indexPath.row])
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
[...]
switch selectionState {
case .Selected(let object as! Object):
if dataSource[indexPath.row] == object { cell.selected = true }
else { cell.selected = false }
default : cell.selected = false
}
}