按钮单击时的tableViewController

时间:2011-10-24 18:54:53

标签: iphone objective-c xcode

我有tableViewController,单元格包含一个按钮和一个标签。当用户点击按钮时,我需要获取单元格的文本(实际上是单元格Person的对象)。

当用户点击按钮时,会执行以下方法;

-(void) buttonOfCellClicked{
      // here i need to access the `Person` object that the user clicked
 }

我如何编写此代码?

编辑:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

Person * person = [personsArr objectAtIndex:indexPath.row];

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell =  [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] ;

label = [[UILabel alloc]initWithFrame:CGRectMake(15, 5, 75, 60)];
label.text =person.firstName;   
[cell addSubview:label];
UIButton *button= [UIButton buttonWithType:UIButtonTypeRoundedRect];

[button addTarget:self action:@selector(buttonOfCellClicked) forControlEvents:UIControlEventTouchUpInside];

[cell.contentView addSubview:button];


}

4 个答案:

答案 0 :(得分:6)

id findAncestor(UIView *view, Class class) {
    while (view && ![view isKindOfClass:class])
        view = [view superview];
    return view;
}

- (void)buttonOfCellClicked:(UIButton *)button
{
    UITableViewCell *cell = (UITableViewCell *)findAncestor(button, [UITableViewCell class]);
    UITableView *table = (UITableView *)findAncestor(cell, [UITableView class]);
    NSIndexPath *path = [table indexPathForCell:cell];
    if (!path)
        return;
    Person *person = [personsArr objectAtIndex:path.row];
    // do whatever with person
}

答案 1 :(得分:0)

我会继承UITableviewCell,并将属性和操作添加到该类,并在IB中连接按钮。配置单元格时,只需设置person对象。这种方法在很大程度上取决于您对该对象的计划。

答案 2 :(得分:0)

你需要问你的按钮告诉你它在哪里。为此,您需要调用superview两次(1x将返回cell.contentView,2x将返回单元格):

- (void)buttonOfCellClicked:(UIButton *)sender {
    UITabvleViewCell *cell = (UITableViewCell *)[[sender superview] superview];
}

然后您可以访问单元格的属性,例如cell.textLabel.text


编辑:您还需要在:文字之后-addTarget:action:forControlEvents:cellForRow添加buttonOfCellClicked

[button addTarget:self action:@selector(buttonOfCellClicked:) forControlEvents:UIControlEventTouchUpInside]

EDIT2:您可以通过

访问此人
NSIndexPath *ip = [yourTableView indexPathForCell:cell];
Person *p = [personArr objectAtIndex:ip.row];

答案 3 :(得分:-1)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [yourArray count];\
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell =  [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] ;

UIButton *button= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTag:indexPath.row]; // SET TAG TO YOUR BUTTON, IT WILL BE SAME AS YOUR OBJECT AT INDEX OF ARRAY
[button addTarget:self action:@selector(buttonOfCellClicked:) forControlEvents:UIControlEventTouchUpInside];

[cell.contentView addSubview:button];


}

-(void) buttonOfCellClicked:(id) sender{
     Person *person = [yourArray objectAtIndex:[sender tag]];
      // here i need to access the `Person` object that the user clicked
 }