2015-12-04 53 views
0

我创建了一个自定义表格视图单元格,其上有一个按钮,并在同一个文件tableviewcell.m(基本calsee UITableViewCell)中的按钮targetof。如何从发件人的选项中获得值选项

当我点击按钮然后行动被调用。

我要做的是从该按钮的发件人选项中获取值。如图所示想要获取“badgename”的值。

enter image description here

+0

ü希望的是得到的值标签在包含该按钮的单元格中,我是否正确? – Tj3n

+0

是的,我想获得标签名称“badgesname”的价值 –

+1

哦,我的..分开存储所有东西。你应该做的是休闲委派模式,并将数据保存在单元格中。然后,当按下按钮时,可以使用self(从单元格)和单元格外部调用委托方法,以获取单元格的数据。 –

回答

0

你可以这样做

UITableViewCell *cell = (UITableViewCell *)[button superView] superView]; 

那么你可以得到你的对象是这样

UILabel *label = (UILabel *)[cell viewWithTag:1234]; 
+1

请不要这样做。 –

+0

是否有任何问题...请告诉我,以便我可以更新自己。 –

+0

看看我的答案,我会扩展它。 –

0

首先,在按钮动作得到全细胞,转换点按钮到tableview的indexPath,那么你可以使用该indexPath从你的数据数组中获取值,甚至可以从单元格中获取数值

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition]; 
    if (indexPath != nil) 
    { 
     //Do stuff with the data array 
     //Object *obj = [dataArray objectAtIndex:indexPath.row] 
     //Get the text from the array 

     //OR 

     //YourCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];  
     //NSString *labelString = cell.textLabel.text; 
    } 
0

Donot在tablviewcell.m文件中创建目标方法。只需创建一个按钮的出口并在基本控制器中添加目标,从创建表的位置开始并按照描述的过程进行操作。
在你的cellForRowAtIndexPath:方法给按钮标签与indexPath.row和按钮的操作方法只是获取标记,并调用的cellForRowAtIndexPath得到cell.By细胞可以获取

-(void)BtnOfCellTapped:(id)sender 
{ 
    UIButton *btn = (UIButton*)sender; 
    CustomCell *cell = (CustomCell*)[tblView cellForRowAtIndexPath:btn.tag]; 
    NSLog(@"%@",cell.badgeValue); 
} 
+0

你认为会发生什么,如果表有更多的一个部分 – sage444

1

你应该当你想要的所有值跟踪来自单元格的一些其他事件,而不仅仅是默认选择。例如你添加一些按钮。

  • 使用UIViewtag属性是用标签阅读代码并不容易。当你不仅有行而且还有段时,你会做什么?
  • 查找UITableViewCell作为 superview您的按钮是不好太按钮可以直接添加为UITableViewCell子视图或作为单元的contentView子视图。
  • 不要试图通过检查触摸点来确定单元格,它是不好的没有评论..
  • 不要在这里使用关联的对象,它的真的很差你可以阅读有关运行反图纹这里的文章:http://nshipster.com/associated-objects/

那么我们该怎么办?我们应该遵循委托模式。它的也缩放。您可以将链接存储到您的单元格中的任何数据。

您单元的标题:

@class CellWithButton; 

@protocol CellWithButtonDelegate <NSObject> 

- (void)cellWithButtonDidPressed:(CellWithButton *)cell; 

@end 

@interface CellWithButton : UITableViewCell 

@property id anyData; 
@property (nonatomic, weak) id <CellWithButtonDelegate> *delegate; 

@end 

您单元的实现:

- (void)buttonPressed:(id)sender 
{ 
    [self.delegate cellWithButtonDidPressed:self]; 
} 

现在你的控制器:

- (UITableViewCell *)tableView cellFor... 
{ 
    CellWithButton *cell = ...; 
    cell.anyData = myData; 
    cell.delegate = self; 
} 

- (void)cellWithButtonDidPressed:(CellWithButton *)cell 
{ 
    id myData = cell.anyData; 
    // Wow! Now you have your data. 
    // And you can even find `indexPath` for your cell 
    // by calling [self.tableView indexPathForCell:cell]. 
} 
+0

你的答案是正确的。但我想你应该提供更多的解释,不仅代码 – sage444

+0

@ sage444我会的,谢谢。 –

+0

@ sage444我应该添加一些东西吗? –

0

您可以使用关联的对象这一点。

如何使用关联的对象?

要设置属性

objc_setAssociatedObject(self, @"AnyString", object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

要获取属性

objc_getAssociatedObject(self,@"AnyString");

参考:http://nshipster.com/associated-objects/

+0

你有没有读过反模式部分? –