2013-09-05 37 views
27

Tôi đang cố gắng để tìm chọn UITableViewCell như sau:iOS, Cách tìm hàng đã chọn của 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]; 


    } 

Làm thế nào để đạt được điều này, bạn có thể xin vui lòng hướng dẫn cho tôi, nơi tôi đang làm sai lầm.

+0

đâu ** didselectrowatindexpath ** phương pháp ??? –

+0

bạn muốn tinh chỉnh ô đã chọn của UITableview ở đâu ..? tại ** didselectrowatindexpath ** hoặc nút TouchUiInside Method? –

+0

Vui lòng kiểm tra dòng "if ([dòng indexPath] == [tableView cellForRowAtIndexPath: indexPath])". Ở đây bạn đang so sánh một NSInteger với một UITableViewCell! – AlexVogel

Trả lời

15

Sử dụng đại biểu phương pháp này UITableView

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

trong UITableViewDataSource đại biểu có một phương pháp - (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath.

Nó trả về đối tượng NSIndexPath có chứa cả phần được chọn và hàng đã chọn.

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

hãy chắc chắn để thiết lập nguồn dữ liệu của tableview trước khi sử dụng, nếu không thì phương pháp này sẽ không được gọi

1

Dưới đây là được xây dựng trong phương pháp đó sẽ giúp bạn xác định tế bào đã được chọn.

- (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); 
} 

và bằng phương pháp đã được cung cấp khi bạn tạo TableViewController, bạn có thể chỉ cần bỏ ghi chú đó.

Hy vọng bạn tìm thấy nó hữu ích

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

tại sao bạn không chấp nhận điều này như là câu trả lời đúng? @pratap Manish Bhadoria –

+0

Methinks @PratapManishBhadoria quên kiểm tra câu trả lời này. –

+0

@SurajKThomas cuz Nó không hoạt động! –

0

Tôi nghĩ rằng những gì bạn đang cố gắng làm là không để có được những chỉ số của tế bào xem bảng nhấp nhưng để biết được tế bào được nhấp.

Để biết điều này tại thời điểm chỉ định mỗi ô gán giá trị indexpath.row làm thẻ của ô được tạo, sau đó nhấp vào xem chế độ xem bảng làm cha mẹ và cố gắng xem subview (ô) của nó tag

[tableView xem với tag: indexpath.row]

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