30

私はUITableViewCell次のように選択されたものを見つけようとしています:

 - (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]; 


    }

これを達成する方法を教えてください。私がミスをしている場所を教えてください。

4

6 に答える 6

108

オブジェクティブ C

 NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];

スウィフトコード

let indexPathForSelectedRow = self.tableView.indexPathForSelectedRow
于 2014-02-27T08:49:55.357 に答える
16

のこのデリゲートメソッドを使用します。UITableView

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSLog(@"%d", indexPath.row); // you can see selected row number in your console;
}
于 2013-09-05T13:14:58.557 に答える
1

あなたがしようとしているのは、クリックされたテーブルビューセルのインデックスを取得することではなく、クリックされたセルを知ることだと思います。

各セルの割り当て時にこれを知るには、作成されたセルのタグとしてindexpath.row値を割り当て、クリックすると、テーブルビューを親として取得し、そのタグでそのサブビュー(セル)を取得しようとします

[タグ:indexpath.row を使用したテーブルビュー ビュー]

于 2014-03-03T13:24:06.883 に答える