0

グループ化された UITableView (これには 4 つのセクションがあります) と、それぞれに複数の行があります。各セルに 2 つのボタンをプログラムで作成しました。

これが私が作成している方法ですUITableViewCell。このコードでは、押されたボタンの indexPath.row と indexPath.section を検出してメソッドに渡そうとしています。どうすればこれを行うことができますか?

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    UITableViewCell *cell ;//= [tableView dequeueReusableCellWithIdentifier:nil];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}


NSString *docTitle = [currentDocument objectForKey:@"displayname"];

UIView *cellView = [[UIView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x+5, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
UILabel *cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(cellView.frame.origin.x + 5, cellView.frame.origin.y + 5, cellView.frame.size.width - 10, 25)];
cellTitle.backgroundColor = [UIColor clearColor];
cellTitle.text = docTitle;
[cellView addSubview:cellTitle];

[cell.contentView addSubview:cellView];

UIButton *viewDocumentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[viewDocumentButton setTitle:@"View Online" forState:UIControlStateNormal];
viewDocumentButton.frame = CGRectMake(cellView.frame.origin.x + 5,       cellTitle.frame.origin.y + cellTitle.frame.size.height + 5, 150, 35);
[viewDocumentButton addTarget:self action:@selector(openDocumentButtonPressedMethod:) forControlEvents:UIControlEventTouchDown];
[viewDocumentButton setTag:indexPath.row];
[cell.contentView addSubview:viewDocumentButton];

UIButton *downloadDocumentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[downloadDocumentButton setTitle:@"Download Document" forState:UIControlStateNormal];
downloadDocumentButton.frame = CGRectMake(cellView.frame.origin.x + 5, viewDocumentButton.frame.origin.y + viewDocumentButton.frame.size.height + 5, 150, 35);
[downloadDocumentButton addTarget:self action:@selector(openDocumentButtonPressedMethod:) forControlEvents:UIControlEventTouchDown];
[downloadDocumentButton setTag:indexPath.row];
[cell.contentView addSubview:downloadDocumentButton];

cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;  
}
4

3 に答える 3

0

のサブクラスを作成しますUIButton

@interface MyButton : UIButton 
{ /* indexPath, other stuff */ }
@end

次に実装

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event

このメソッドindexPathの を呼び出す前に、 およびその他の処理を実行します。super

ボタンを作成するときに、 を指定しますindexPath

于 2013-04-20T00:46:18.593 に答える
0

あなたのopenDocumentButtonPressedMethod:方法では、

CGPoint pointInTableView = [self.tableView convertPoint:sender.bounds.origin fromView:sender];

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInTableView];
于 2013-04-20T00:49:38.733 に答える