How to remember rows selected and maintain them after reload by default in UITableView?(UITableView中默认重新加载后如何记住选择的行并维护它们?)
问题描述
如何设置默认选中的行?我想选择一行,使用 indexpath.row
获取所选行的数量,然后重新加载表格并选择所选行.有人能帮助我吗?我可以在方法 -(IBAction
) segmentedControlIndexChanged
中获取选定的行吗?不在 didSelectRowAtIndexPath
中?
How can I set a row selected by default? I want to select a row , take the number of selected row with indexpath.row
and then reload the table and select the row what was selected. Can someone help me please? Can I get the selected row in a method -(IBAction
) segmentedControlIndexChanged
? Not in didSelectRowAtIndexPath
?
这是我的代码,如果它可以帮助你理解
This is my code if it can help you to understand
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 100, 20) ];
// Configure the cell...
NSString *depdateChoosed=[deparatureDates objectAtIndex:depSelectedIndice];
NSString *comeateChoosed=[deparatureDates objectAtIndex:comSelectedIndice];
NSString *choosedDate =[NSString stringWithFormat:@"%@%@",depdateChoosed, comeateChoosed];
NSLog(@"date choisi %@ ",choosedDate);
if(segment.selectedSegmentIndex==0)
{
cell.textLabel.text =[deparatureDates objectAtIndex:indexPath.row];
}
else if(segment.selectedSegmentIndex==1) {
//cell.textLabel.text =[goingBackDates objectAtIndex:indexPath.row];
//cell.textLabel.text=[goingBackDates objectAtIndex:indexPath.row];
cell.textLabel.text =[goingBackDates objectAtIndex:indexPath.row];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@" champ séléctionner : %d ",indexPath.row);
if(segment.selectedSegmentIndex==0)
{
depSelectedIndice=indexPath.row;
}
else if(segment.selectedSegmentIndex==1) {
comSelectedIndice=indexPath.row;
}
//[tableView reloadData];
}
-(IBAction) segmentedControlIndexChanged
{
int i;
switch (self.segment.selectedSegmentIndex)
{
case 0:
i=0;
[tableView reloadData];
break;
case 1:
i=1;
NSLog(@"dexieme segment selectionner");
[tableView reloadData];
default:
break;
}
}
推荐答案
NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
将为您提供当前所选行的 NSIndexPath
.然后你可以做
will give you the NSIndexPath
for the current selected row. You can then do
[tableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionMiddle];
稍后选择该行(并将其显示在屏幕中间).
to select that row (and show it in the middle of the screen) later.
这篇关于UITableView中默认重新加载后如何记住选择的行并维护它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UITableView中默认重新加载后如何记住选择的行并维护它们?
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01