小编Dex*_*ter的帖子

按钮单击时的tableViewController

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

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

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

我该如何编写这段代码?

编辑:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Run Code Online (Sandbox Code Playgroud)

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


}
Run Code Online (Sandbox Code Playgroud)

iphone xcode objective-c

1
推荐指数
1
解决办法
637
查看次数

标签 统计

iphone ×1

objective-c ×1

xcode ×1