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

问题描述:

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

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

} 

感谢。

+1

请显示'actionMore:'方法的代码。 – dasblinkenlight 2012-08-13 16:06:50

+0

我已添加actionMore的详细信息。 – Aymeric 2012-08-13 16:36:18

为了帮助您需要提供更多的细节。

  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语句找到该行被触发。

+0

它是多细胞的原型。我已经添加actionMore的详细信息。了解如何修改一个单元格就是一个简单的例子。 – Aymeric 2012-08-13 16:35:08

+0

为您的代码添加了答案。 – RMDan 2012-08-13 16:54:21

你可以在该按钮与代码行之后按下电池

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]; 

由于您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 
} 
+0

这是正确的答案。你应该利用视图标记来使你的cellForRowAtIndexPath更高效。即使在返回可重用的单元时,通过重新创建UIView,UILabel和UIButton,您也击败了单元重用的目的。使用相同的viewWithTag技巧从可重用单元格获取视图,标签和按钮,然后根据需要设置属性。 – CSmith 2012-08-13 18:02:36

你更好添加view为一体的contentview

一个subview传递indexpathactionMore或从那里获取它。一旦你得到了indexpath,你就有生意。

您可能需要为您的textlabel设置一个标记,以便从superview中获取标记。

- (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"]; 
} 
+0

你可以跳过'superview/subview'跳转:'UITableViewCell * view =(UIView *)[sender superview]; UILabel * label = [view.subviews objectAtIndex:0];'我认为基于标签的解决方案比基于索引的解决方案更强大。 – dasblinkenlight 2012-08-13 19:12:25