单击按钮时的 tableViewController

发布于 2024-12-11 19:12:09 字数 1106 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱,才寂寞 2024-12-18 19:12:09
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
}
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
}
不再让梦枯萎 2024-12-18 19:12:09

我将子类化 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.

岁月静好 2024-12-18 19:12:09

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

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

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


编辑:您还需要在 buttonOfCellClicked 之后的 cellForRow 中将 : 添加到 -addTarget:action:forControlEvents:文本:

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

EDIT2:您可以通过以下方式访问人员

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

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):

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

Then you can access cell's properties e.g. cell.textLabel.text


EDIT: You also need to add : to your -addTarget:action:forControlEvents: in cellForRow after buttonOfCellClicked text:

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

EDIT2: you can access person by

NSIndexPath *ip = [yourTableView indexPathForCell:cell];
Person *p = [personArr objectAtIndex:ip.row];
长发绾君心 2024-12-18 19:12:09
- (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
 }
- (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
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文