use*_*567 29 iphone objective-c ios4 ios
如何设置默认选择的行?我想选择一行,获取所选行的数量,indexpath.row
然后重新加载表并选择所选行.有谁可以帮助我吗?我可以在方法中获取所选行 - (IBAction
)segmentedControlIndexChanged
吗?不在didSelectRowAtIndexPath
?
这是我的代码,如果它可以帮助您理解
- (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;
}
}
Run Code Online (Sandbox Code Playgroud)
Sea*_*ell 64
NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
Run Code Online (Sandbox Code Playgroud)
将为您NSIndexPath
提供当前所选行.然后你可以做
[tableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionMiddle];
Run Code Online (Sandbox Code Playgroud)
稍后选择该行(并在屏幕中间显示).
xls*_*rlx 30
我有同样的问题,这就是我做的方式:
在你的tableViewController
,在这个功能中添加它
(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 'the row you want to select')
{
[tableView
selectRowAtIndexPath:indexPath
animated:TRUE
scrollPosition:UITableViewScrollPositionNone
];
[[tableView delegate]
tableView:tableView
didSelectRowAtIndexPath:indexPath
];
}
}
Run Code Online (Sandbox Code Playgroud)
小智 9
这样做:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition: UITableViewScrollPositionNone];
Run Code Online (Sandbox Code Playgroud)
其中row是要选择的单元格编号(从0开始...),section是单元格出现的部分.
这是您在UITableView中预选行的方法:
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition: UITableViewScrollPositionNone];
Run Code Online (Sandbox Code Playgroud)
将indexPath设置为要选择的行,然后按上述方法调用方法.