2013-09-05 17 views
27

Próbuję znaleźć wybrany UITableViewCell następująco:iOS, Jak znaleźć wybrany wiersz z 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]; 


    } 

Jak to osiągnąć, można kierować mi proszę, gdzie robię błąd.

+0

Gdzie ** didselectrowatindexpath ** metoda ??? –

+0

gdzie chcesz wykończyć wybraną komórkę z UITableview ..? na ** didselectrowatindexpath ** lub przycisk TouchUiInside Method? –

+0

Sprawdź także wiersz "if ([wierszPathPath] == [tableView cellForRowAtIndexPath: indexPath])". Tutaj porównujesz NSInteger z UITableViewCell! – AlexVogel

Odpowiedz

15

Użyj tego delegat metoda UITableView

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

w UITableViewDataSource delegata istnieje metoda - (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath.

Zwraca obiekt NSIndexPath, który zawiera zarówno wybraną sekcję, jak i zaznaczony wiersz.

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

upewnij się, aby ustawić źródło danych z tableview przed użyciem go w przeciwnym razie ta metoda nie zostanie wywołana

1

oto wbudowanej metody, które pomogą Ci określić, która komórka została wybrana.

a przy okazji metoda jest już podana podczas tworzenia TableViewController, prawdopodobnie po prostu trzeba odkomentować go.

Mam nadzieję, że pomocne

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

dlaczego nie akceptujesz tego jako poprawnej odpowiedzi? @pratap Manish Bhadoria –

+0

Methinks @PratapManishBhadoria zapomniałem sprawdzić tę odpowiedź. –

+0

@SurajKThomas bo to nie działa! –

0

myślę, co chce zrobić, to nie dostać indeks komórce widoku tabeli kliknął jednak wiedzieć, które komórka kliknięciu.

Aby dowiedzieć się o tym w momencie przypisywania każdej komórki, przypisz wartość indexpath.row jako znacznik komórki utworzonej po kliknięciu, podejmij widok tabeli jako obiekt nadrzędny i spróbuj uzyskać jego podgląd (komórkę) z tą komórką oznacz

[widok tableview tagiem: indexpath.row]

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