2013-09-05 25 views
27

我試圖找到選定UITableViewCell如下:iOS,如何找到選定的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]; 


    } 

如何實現這一點,可以請你指導我,在那裏我做的錯誤。

+0

在哪裏** ** didSelectRowAtIndexPath方法方法??? –

+0

你想在哪裏罰款UITableview的選定單元..?在** didselectrowatindexdex **或按鈕TouchUiInside方法? –

+0

也請檢查「if([indexPath row] == [tableView cellForRowAtIndexPath:indexPath])」行。在這裏你將一個NSInteger與一個UITableViewCell進行比較! – AlexVogel

回答

15

使用在UITableViewDataSource委託UITableView

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

委託方法有一個方法- (空)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath

它返回NSIndexPath對象,其中包含選定的部分和選定的行。

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

確保使用它,否則此方法將不會被調用

1

下面是內置的方法,可以幫助你確定哪些細胞已被選擇之前設置的tableview的數據源。

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

並且順便說一下,當您創建TableViewController時已經給出該方法,您可能只需取消它的註釋。

希望你覺得它有用

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

你爲什麼不接受這個正確答案? @pratap Manish Bhadoria –

+0

Methinks @PratapManishBhadoria忘了檢查這個答案。 –

+0

@SurajKThomas因爲它不工作! –

0

我認爲你正在嘗試做的是不要讓點擊,但知道的是單擊的單元格的表格視圖單元格的指數。

要分配每個單元格時分配indexpath.row value作爲創建的單元格的標記,然後單擊將表視圖作爲父級,並嘗試獲取它的子視圖(單元格)標記

[泰伯維視圖與標籤:indexpath.row]

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