gpt4 book ai didi

ios - 检测在分组的 UITableView 中单击了哪个按钮

转载 作者:行者123 更新时间:2023-11-28 19:10:30 25 4
gpt4 key购买 nike

我有一个分组的 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;
}

最佳答案

另外,你可以...

- (IBAction)openDocumentButtonPressedMethod:(id)sender {
UIButton *button = (UIButton *)sender;
UIView *contentView = button.superview;
UITableViewCell *cell = (UITableViewCell *)contentView.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

// do something with the indexPath
}

如果您想使用相同的方法来处理两个按钮事件,那么您可以使用按钮上的标签来区分这两个按钮。

if (button.tag == 1) {
// do something with the indexPath view related
}
else {
// do something with the indexPath download related
}

关于ios - 检测在分组的 UITableView 中单击了哪个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16115203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com