xcode 通过 reloadData 更新 UITableView 时保持选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9791507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Keeping selection upon updating UITableView via reloadData
提问by Jerome
I am trying to implement a view similar to that of Apple's calendar application's setting the start time and end time view. I have the view looking great, but I am running into two problems. First, I want to have the first row selected automatically. I sort of have this working using:
我正在尝试实现类似于 Apple 日历应用程序设置开始时间和结束时间视图的视图。我的视野很好,但我遇到了两个问题。首先,我想自动选择第一行。我有点使用这个工作:
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
[dateTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
However, an animation shows, visually selecting the rown when the view is loaded, I want it to already be selected when the view loads.
但是,动画显示,在加载视图时直观地选择行,我希望它在视图加载时已经被选中。
The second, and much more important, problem is that when I reload the data after the picker has updated I lose the selection, and my task simply doesn't work.
第二个也是更重要的问题是,当我在选择器更新后重新加载数据时,我失去了选择,我的任务根本不起作用。
Now I know this is the default action of reloadData, I am looking for an alternative method to accomplish my goal, or perhaps how I can augment reloadData to not deselect the current selection.
现在我知道这是 reloadData 的默认操作,我正在寻找一种替代方法来实现我的目标,或者我可能如何增加 reloadData 以不取消选择当前选择。
My task is included below:
我的任务包括如下:
-(IBAction)dateChanged
{
NSIndexPath *index = self.dateTableView.indexPathForSelectedRow;
if(index == 0)
{
if (self.date2 == plusOne ) {
self.date = [datePicker date];
plusOne = [self.date dateByAddingTimeInterval:60*60];
self.date2 = plusOne;
}
else
{
self.date = [datePicker date];
}
}
else{
self.date2 = [datePicker date];
}
[dateTableView reloadData];
}
Note: plusOne is a variable that initially indicates an hour from the current time.
注意:plusOne 是一个变量,最初表示从当前时间开始的一个小时。
Thanks in advance!
提前致谢!
回答by Brian Cooley
For the first problem, set animated:NO on the method call. You are currently setting it to YES.
对于第一个问题,在方法调用上设置animated:NO。您当前将其设置为 YES。
[dateTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom];
For the second problem, it's not really clear what you are trying to select after reloading the data, but I see a problem with this line:
对于第二个问题,您在重新加载数据后尝试选择的内容并不是很清楚,但我发现这一行存在问题:
if(index == 0)
You are asking if the pointer to the object is 0. I think what you want is either to check that index == nil
or that index.section == 0 && index.row == 0
or something like that.
您问的是指向对象的指针是否为 0。我认为您想要的是检查那个index == nil
或那个index.section == 0 && index.row == 0
或类似的东西。
Anyway, if you call reloadData
on the UITableView, you're going to lose the selection. At that point, you need to select a new row. If there is an item in your data model that you want to select, you need to figure out where it is and select it based on where it will be in the table (You should know this because you are providing that information in the UITableViewDataSource delegate methods.). Alternatively, if you want to select the NSIndexPath you saved in the first line of dateChanged
, just select it after reloading the data.
无论如何,如果您调用reloadData
UITableView,您将失去选择。此时,您需要选择一个新行。如果您的数据模型中有您想要选择的项目,您需要找出它的位置并根据它在表格中的位置选择它(您应该知道这一点,因为您在 UITableViewDataSource 委托中提供了该信息方法。)。或者,如果要选择第一行中保存的 NSIndexPath dateChanged
,只需在重新加载数据后选择它即可。
回答by rakeshNS
For the first problem, write your code in didSelectRowAtIndexPath: in a method. You can then call this method from didSelectRowAtIndexPath: You should pass indexPath as argument to your function like,
对于第一个问题,在 didSelectRowAtIndexPath: 方法中编写代码。然后,您可以从 didSelectRowAtIndexPath 调用此方法:您应该将 indexPath 作为参数传递给您的函数,例如,
-(void)doActionsInDidSelectRow:(NSIndexPath*)indexPath{
// write code.
}
In viewDidLoad call this method as
在 viewDidLoad 中调用此方法为
-(void)viewDidLoad{
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
[dateTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
[self doActionsInDidSelectRow:indexPath];
}
For the second problem my suggestion is, each time when you selecting a cell store that cell's text in a NSString. When you reload data, inside cellForRowAtIndexPath: just compare cell's text with the string content you stored previously. If they equal just make selection using
对于第二个问题,我的建议是,每次选择一个单元格时,都会将该单元格的文本存储在 NSString 中。当您重新加载数据时,在 cellForRowAtIndexPath 中:只需将单元格的文本与您之前存储的字符串内容进行比较。如果它们相等,只需使用
[dateTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
method. Hope this will solve your issues.
方法。希望这能解决您的问题。