¿Cómo puedo establecer una fila seleccionada de forma predeterminada? Quiero seleccionar una fila, tomar el número de la fila seleccionada con indexpath.row
y luego volver a cargar la tabla y seleccionar la fila que fue seleccionada. ¿Puede alguien ayudarme por favor? ¿Puedo obtener la fila seleccionada en un método - (IBAction
) segmentedControlIndexChanged
? No en didSelectRowAtIndexPath
?¿Cómo recordar las filas seleccionadas y mantenerlas después de volver a cargarlas por defecto en UITableView?
Este es mi código si puede ayudar a entender
- (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;
}
}
método NSIndexPath indexPath = [tableView indexPathForSelectedRow]; no puede devolver int? – user567
error tipográfico, lo siento. es un puntero a una ruta de índice. –
Además, asegúrese de tener 'clearsSelectionOnViewWillAppear' en su' UITableViewController' establecido en 'NO', de lo contrario, desactivará automáticamente la fila cuando aparezca la vista. Esto me estaba molestando por horas. – cheeesus