4

動的なtableViewと1つの問題があります。各tableCellには2つのボタンがあります。ボタンのindexPath.rowをタッチしようとしていますが、成功しません。

私のコード:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    NSIndexPath *indexPath = [self.homeTableView indexPathForSelectedRow];      

    NSLog(@"%d", indexPath.row);


}

toucedボタンのindexPath.rowを取得するにはどうすればよいですか(Segue接続はこの2つのボタン-> 2つの接続上にあります)?

4

6 に答える 6

10

ボタンをクリックしたときに行を選択しないため、機能しません。

2つのボタンを使用して、セグエをボタンから切断し、IBで2つの手動セグエを実行してから、そのようなコードを追加してそれらを処理します。

-(void)button1Pressed:(id)sender {
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *clickedButtonIndexPath = [self.homeTableView indexPathForCell:clickedCell];
    ...
    [self performSegueWithIdentifier:@"YourManualSegueIdentifier1" sender:self];
}

2番目のオプションの編集:

MyTableViewCell.h内:

@interface MyTableViewCell : UITableViewCell
    ...    
    @property (strong, nonatomic) NSIndexPath *cellIndexPath;
    ...
@end

MyTableViewCell.m内:

-(void)button1Pressed:(id)sender {
    ...
    NSInteger row = cellIndexPath.row;
    ...
}

MyTableViewController.m内:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];      
    }
    // remember index here!
    [cell setCellIndexPath:indexPath];
    ....
    return cell;
}
于 2012-10-22T15:45:51.883 に答える
10

AppleはiOS7でUITableViewCell階層を変更します

iOS6.1SDKの使用

<UITableViewCell>
   | <UITableViewCellContentView>
   |    | <UILabel>

iOS7SDKの使用

<UITableViewCell>
   | <UITableViewCellScrollView>
   |    | <UITableViewCellContentView>
   |    |    | <UILabel>

したがって、ボタンインデックスでインデックスパスを取得する場合は、iOS6以降のSDKを使用して次のコードを使用します

UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *clickedButtonPath = [book_table indexPathForCell:clickedCell];
    NSLog(@"index=%@",clickedButtonPath);

iOS7または7+SDKの使用

 UITableViewCell *clickedCell = (UITableViewCell *)[[[sender superview] superview]superview];
    NSIndexPath *clickedButtonPath = [book_table indexPathForCell:clickedCell];
    NSLog(@"index=%ld",(long)clickedButtonPath.item);
于 2013-10-07T06:55:38.893 に答える
6

私はストーリーボードを使用していませんが、単純なビューコントローラーベースのプロジェクトで使用した後は、これが役立つと思います。

 - (IBAction)goToNew:(id)sender 
{
   UIButton *btn = (UIButton*) sender;
   UITableViewCell *cell = (UITableViewCell*) [btn superview];
   NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 
   int row = indexPath.row;
}
于 2012-10-22T15:38:14.953 に答える
1

たぶん、UITableView/UITableViewCellの代わりにUIButtonにタッチが発生しました。

確認する :

NSLog(@"%@",[sender class]);
于 2012-10-22T15:32:35.613 に答える
0

button.tagで解決

「cellForRowAtIndexPath」でbutton.tag=indexPath.rowを設定し、次に「-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{」

UIButton *btn = (UIButton*) sender;
NSLog(@"%d", btn.tag);
于 2012-10-22T17:21:28.890 に答える
0

与えるボタンtag = indexpath.row+100;

と取得..

-(IBAction)ShowListView:(id)sender
{

    UIButton *btn = (UIButton *)sender;

    int Iindex = btn.tag-100;

    NSLog(@"button title is %@",btn.titleLabel.text);
    NSLog(@"button id is %d",Iindex);

    NSLog(@"array category is....%@",arrayCategory);
于 2013-10-07T06:59:51.493 に答える