如何设置默认选择的行?我想选择一行,将所选行的编号与indexpath.row
对齐,然后重新加载表并选择所选行。有人能帮助我吗?我可以在方法中获取选定的行吗?(IBAction
)segmentedControlIndexChanged
?不在didSelectRowAtIndexPath
?如何在UITableView中默认重新加载后记住选定的行并对其进行维护?
这是我的代码,如果它可以帮助你了解
- (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];不能返回int? – user567 2011-05-25 00:09:38
错字,对不起。它是一个指向索引路径的指针。 – 2011-05-25 00:10:30
另外,确保你的'UITableViewController'上的'clearsSelectionOnViewWillAppear'设置为'NO',否则当视图出现时它会自动取消选择该行。这让我困扰了好几个小时。 – cheeesus 2013-04-06 12:08:04