ios - UITableview 中的按钮修改当前单元格自定义

标签 ios uitableview uibutton

我找不到修改我的自定义单元格的解决方案,因为里面有一个按钮。我希望在单击按钮时更改标签文本。

- (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:nil];
}

// View
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)];
[view setBackgroundColor:[UIColor colorWithWhite:255.0 alpha:0.0]];

// Label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320-7-7, 16)];
[label setText:@"Hello"];
[label setTextAlignment:UITextAlignmentLeft];
[label setTextColor:[UIColor grayColor]];
[label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:16]];
[label setBackgroundColor:[UIColor clearColor]];
[view addSubview:label];

// Action
UIButton *action = [[UIButton alloc] initWithFrame:CGRectMake(306-54, 0, 54, 54)];
[action setTitle:@"0" forState:UIControlStateNormal];
[action addTarget:self action:@selector(actionMore:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:action];

// Add view to cell
[cell addSubview:view];

return cell;
}

编辑(添加详细信息)

- (void)actionMore:(id)sender{
UIButton *button = (UIButton *)sender;

// Edit current cell, but I do not know how...

}

谢谢。

最佳答案

您可以使用以下代码行获取按下按钮的单元格

UITableViewCell * cell = (UITableViewCell*)[[sender superview] superview];

然后使用单元格引用更改标签文本

cell.yourlabel.text = @"New Text";

更新 我认为上面的代码在 iOS 7 中可能不起作用。更好的方法是

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.mainTable];
NSIndexPath *indexPath = [self.mainTable indexPathForRowAtPoint:buttonPosition];

关于ios - UITableview 中的按钮修改当前单元格自定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11938027/

相关文章:

ios - 无法将数据写入 plist 文件

ios - 为什么观点没有改变? Xcode/swift 2

ios - Swift - 如何在带参数的方法上调用 Selector()?

ios - 添加初始注释

ios - UITableViewCell 删除内容

swift - 如何消除 UIButton 图像和 UIButton 边框之间的填充?

ios - 按钮导致崩溃

ios - 在自定义单元格中的按钮中设置标签时出现异常

iOS 获取一个 UITableViewCellAccessoryDe​​tail 的 View

ios - 如何在不重叠的情况下在 UIButton 中实现两个 IBAction?