2012-09-26 19 views
19

Tengo un botón y una Tabla. Ahora quiero hacer clic de tal manera que cada vez que seleccione una fila en tableview y presione el botón, ese evento de pulsación de botón en particular ocurra. Para ello, en primer lugar me he darle etiqueta para cada fila es decirHacer clic en Evento en UITableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *LabelCellIdentifier = @"cell"; 
UITableViewCell *cell; 
cell = [tableView dequeueReusableCellWithIdentifier:LabelCellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LabelCellIdentifier]; 


} 

if (indexPath.row <= [_arrayMp3Link count]) { 

    cell.textLabel.text = [_arrayMp3Link objectAtIndex:indexPath.row]; 

    } 

// now tag each row 
NSInteger count = 1; 
for (NSInteger i = 0; i < indexPath.section; i++) { 
    count += [[tableView dataSource] tableView:tableView numberOfRowsInSection:i]; 
} 
count += indexPath.row; 
// dequeue, create and configure... 
cell.tag = count; 



return cell; 
} 

y ahora en poner evento en el botón cuando selecciono la fila y pulse el botón de mi. Pero no obteniendo las cosas correctas.

(IBAction)doDownload:(id)sender { 
// to select first row 
if(cell.tag==1) 
{ 
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]]; 
} 
+0

Puede obtener la fila de la IBAction. Mira mi respuesta aquí: http://stackoverflow.com/a/12594183/1144632 – danielbeard

Respuesta

29

declarar una variable global int -

int rowNo; 

A continuación, asigne valor a la misma en didSelectRowAtIndexPath: método

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    rowNo = indexPath.row; 
} 

Ahora usted tiene un indexNo. de la fila seleccionada

-(IBAction)doDownload:(id)sender 
{ 
    //Now compare this variable with 0 because first row index is 0. 
    if(rowNo == 0) 
    { 
     [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]]; 
    } 
} 
+0

ok gracias! y si quiere la misma acción con cada fila ... ¿necesito usar index path.section? y ¿qué hay que descargar? – Christien

+0

¿Cuántas secciones tiene ??? – TheTiger

+0

solo uno ....... – Christien

5

Tienes que utilizar la función didSelectRowAtIndexPath método de tableView.

en ese método, guarda la etiqueta de fila seleccionada o lo que quiera guardar.y en la acción del botón, verifique el valor de la entidad guardada y haga cualquier cosa.

+0

k pero para seleccionar una fila y presionar el botón que cosa necesito usar – Christien

+0

si quieres la acción cuando seleccionas la fila de la tabla, lo cual es mejor, entonces puedes verificar la etiqueta de la celda dentro del func didiSelectRowAtIndexPath y hacer lo que quieras. Pero si debes usar la selección y el botón, presiona ambos y luego usa ambos. – tausun

2
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if(indexPath.row==i) 
{//Perform whatever action you would like for whichever row number..i'm just using i as an int placeholder 
    [[UIApplication sharedApplication]openURL:[NSURLWithString:@"http://www.google.com"]]; 
} 

//Or you could perform the same action for all rows in a section 
if(indexPath.section==i) 
{ 
[[UIApplication sharedApplication]openURL:[NSURLURLWithString:@"http://www.google.com"]]; 

} 
+0

ya thanx hombre! pero quiero cada vez que selecciono una fila y hago clic en el botón que he dado por separado. – Christien

+0

Lo siento, no entiendo. ¿Estás diciendo que quieres poder hacer lo mismo si haces clic en el botón y en la fila? ¿O qué estás tratando de hacer? –

+0

thanx.Lo tengo ... compruebe si he usado el código correcto para etiquetar cada fila. – Christien

3

utilizar la función de fuente de datos de UITableView que es

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}

cuales indexPath.row es el índice de cada fila.

+0

ya thanx mam! pero quiero cada vez que selecciono una fila y hago clic en el botón que he dado por separado. – Christien

+0

Cuando selecciona una fila, ¿desea que también se seleccione el botón? ¿Estoy en lo cierto? – KarenAnne

1
//use selectedrow to check the condition 

-(void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

        selectedrow=indexPath.row; 
    } 

     -(IBAction)doDownload:(id)sender { 
     // to select first row 
     if(selectedrow==1) 
     { 
      [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]]; 
     } 
+0

gracias hombre! eso realmente me funciona! – Christien

2

'al-entrevista-2.jpg' es el nombre de un archivo de imagen

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 

    ViewController *vc = [sb instantiateViewControllerWithIdentifier:@"viewc"]; 

    [self.navigationController pushViewController:vc animated:YES]; 
    [vc setImageName:@"the-interview-2.jpg"]; 
} 
Cuestiones relacionadas