2013-09-05 10 views
27

sto cercando di trovare selezionato UITableViewCell come segue:iOS, Come trovare la riga selezionata di 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]; 

    } 
    if ([indexPath row] == [tableView cellForRowAtIndexPath:indexPath.row]) // i want to place value of selected row to populate the buttons 

     //([indexPath row] == 0) 

     //(indexPath.row == ![self cellIsSelected:indexPath]) 

    { 
     UIButton *AddComment = [UIButton buttonWithType:UIButtonTypeCustom]; // custom means transparent it takes shape same with backgroup image 

     [AddComment addTarget:self 
         action:@selector(TestBtns:) 
      forControlEvents:UIControlEventTouchDown]; 

     // [AddComment setTitle:@"1" forState:UIControlStateNormal]; 

     AddComment.frame = CGRectMake(9.0, 128.0, 96.0, 26.0); 

     [cell.contentView addSubview:AddComment]; 

     UIImage *buttonAddComment = [UIImage imageNamed:@"addcomment.png"]; 

     [AddComment setBackgroundImage:buttonAddComment forState:UIControlStateNormal]; 

     [cell.contentView addSubview:AddComment]; 


    } 

come raggiungere questo obiettivo, si può per favore mi guida, dove sto facendo Errore.

+0

Dove è ** ** didSelectRowAtIndexPath metodo ??? –

+0

dove volevi affinare la cella selezionata di UITableview ..? a ** didselectrowatindexpath ** o pulsante TouchUiInside Method? –

+0

Controllare anche la riga "if ([indexPath row] == [tableView cellForRowAtIndexPath: indexPath])". Qui stai confrontando un NSInteger con un UITableViewCell! – AlexVogel

risposta

15

Utilizzare questa delegato metodo UITableView

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"%d", indexPath.row); // you can see selected row number in your console; 
} 
6

in UITableViewDataSource delegato c'è un metodo - (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath.

Restituisce l'oggetto NSIndexPath che contiene sia la sezione selezionata sia la riga selezionata.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NSLog(@"Selected section>> %d",indexPath.section); 
    NSLog(@"Selected row of section >> %d",indexPath.row); 
} 

assicurarsi di impostare DataSource del Tableview prima di utilizzarlo altrimenti questo metodo non sarà chiamato

1

Ecco il metodo incorporato che vi aiuterà a determinare quale cella è stata selezionata.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // you can use "indexPath" to know what cell has been selected as the following 
    NSLog(@"Selected row is %@", indexPath); 
} 

e dal modo in cui il metodo è già dato quando si crea TableViewController, è probabilmente solo bisogno di togliere il commento esso.

spero vi sia utile

102
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow]; 
+5

perché non accetti questo come la risposta corretta? @pratap Manish Bhadoria –

+0

Methinks @PratapManishBhadoria ha dimenticato di controllare questa risposta. –

+0

@SurajKThomas cuz Non funziona! –

0

Penso che quello che si sta cercando di fare non è quello di ottenere l'indice della cella di vista tabella cliccato ma per sapere quale cella viene cliccato.

per conoscere questo, al momento di assegnare ogni cella assegnare il valore diindexpath.row come il tag della cella creato quel momento in poi cliccato ritenere tavolo come il genitore e cercare di ottenere la sua visualizzazione secondaria (cellulare) con quella contrassegnare

[vista Tableview con tag: indexpath.row]

2
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    int a = indexPath.row; 
    NSLog(@"index: %i",a); 
} 
Problemi correlati