单击按钮时的 tableViewController
我有一个 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];
}
I have a tableViewController
, the cells contains a button and a label. I need to get the text of the cell (actually the object of the cell Person
) when the user clicks on the button.
When the user clicks on the button the following method gets executed;
-(void) buttonOfCellClicked{
// here i need to access the `Person` object that the user clicked
}
How do i write this code?
EDIT:
- (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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我将子类化 UITableviewCell 并向该类添加属性和操作,然后在 IB 中连接按钮。当您配置单元格时,只需设置人员对象。这种方法在很大程度上取决于您计划对该对象执行的操作。
I would subclass UITableviewCell and add a property and the action to that class and in IB connect the button. When you are configuring the cell just set the person object. This approach would depend heavily on what you plan on doing with that object.
您需要询问按钮来告诉您它在哪里。为此,您需要调用
superview
两次(1x 将返回 cell.contentView,2x 将返回 cell):然后您可以访问单元格的属性,例如
cell.textLabel.text
编辑:您还需要在
buttonOfCellClicked
之后的cellForRow
中将:
添加到-addTarget:action:forControlEvents:
文本:EDIT2:您可以通过以下方式访问人员
You need to ask your button to tell you where it is. To achieve this, you need to call
superview
twice (1x will return cell.contentView, 2x will return cell):Then you can access cell's properties e.g.
cell.textLabel.text
EDIT: You also need to add
:
to your-addTarget:action:forControlEvents:
incellForRow
afterbuttonOfCellClicked
text:EDIT2: you can access person by