【问题标题】:Button in UITableview modify current cell customUITableview中的按钮修改当前单元格自定义
【发布时间】:2012-08-13 16:02:21
【问题描述】:

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

- (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...

}

谢谢。

【问题讨论】:

  • 请出示actionMore:方法的代码。
  • 我已经为 actionMore 添加了详细信息。

标签: ios uitableview uibutton


【解决方案1】:

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

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

然后使用单元格引用来改变标签文字

cell.yourlabel.text = @"新文本";

更新 我认为上面的代码可能无法在 iOS 7 中运行。更好的方法是

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

【讨论】:

    【解决方案2】:

    由于您的UIButtonUILabel 是同一视图的子视图,您可以标记您的标签,并使用viewWithTag 从按钮的操作代码中找到它:

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320-7-7, 16)];
    label.tag = 1234;
    ....
    
    - (void)actionMore:(id)sender{
        UIButton *button = (UIButton *)sender;
        UILabel *label = [[sender superview] viewWithTag:1234];
        // Edit current cell
    }
    

    【讨论】:

    • 这是正确答案。并且您应该利用视图标记来提高 cellForRowAtIndexPath 的效率。通过重新创建 UIView、UILabel 和 UIButton,即使返回了可重用的单元格,您也违背了单元格重用的目的。使用相同的 viewWithTag 技巧从可重复使用的单元格中获取视图、标签和按钮,然后根据需要设置属性。
    【解决方案3】:

    为了帮助您,您需要提供更多详细信息。

    1. 这是单个单元格还是多个单元格的原型?
    2. actionMore: 选择器中的代码。
    3. 您这样做是为了什么?为什么要在单元格中添加一个按钮来更改标签文本?

    如果多次使用此单元格,则问题可能是 actionMore:无法隔离哪个单元格是代码的目标。您也可能在 actionMore 方法中有一个简单的拼写错误,但如果不能查看所有交互代码,我们就无法解决这个问题。

    如果您可以提供更多详细信息,有人可能会提供帮助。如果我有更多信息可以帮助你,我会编辑这个答案。

    --编辑--

    首先获取可以使用的单元格:

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

    然后您可以通过以下方式访问您的单元格标签:

    cell.label.text = @"Some Text Here";
    

    您的下一个问题是找出您的单元格在列表中的哪个位置。为此使用此代码:

    UITableView * table = (UITableView*)[[[sender superview] superview] superview];
    NSIndexPath * indexPath = [table indexPathForCell: cell];
    

    然后您可以使用 switch 语句或 if-else 语句来查找触发了哪一行。

    【讨论】:

    • 它是一个多单元的原型。我已经为 actionMore 添加了详细信息。这是一个了解如何修改一个单元格的简单示例。
    【解决方案4】:
    - (void)actionMore:(id)sender{
        UITableViewCell * cell = (UITableViewCell*)[[sender superview] superview];
    
        UIView *view = [cell.subviews objectAtIndex:0];
        UILabel *label = [view.subviews objectAtIndex:0];
    
        [label setText:@"test"];
    }
    

    【讨论】:

    • 你可以跳过superview/subview 跳转:UITableViewCell * view = (UIView*)[sender superview]; UILabel *label = [view.subviews objectAtIndex:0]; 我认为基于标签的解决方案比基于索引的解决方案更强大。
    【解决方案5】:

    您最好将view 添加为subviewcontentview

    indexpath 传递给actionMore 或从那里获取它。收到indexpath 后,您就开始营业了。

    您可能想为您的textlabel 设置一个标签,以便从其superview 中获取它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多