iPhone 问题:如何向 tableview 单元格添加按钮?

发布于 2024-09-01 10:18:47 字数 1358 浏览 2 评论 0原文

小背景,表视图由 fetchedResultsController 填充,它用字符串填充表视图。现在我尝试在每个表视图单元格中的每个字符串旁边添加一个按钮。到目前为止,我一直在尝试在 configureCell:atIndexPath 方法中创建一个按钮,然后将该按钮作为子视图添加到表单元格的内容视图中,但由于某种原因,这些按钮没有显示。下面是相关代码。如果有人想要发布更多代码,请询问,我会发布您想要的任何内容。非常感谢任何帮助。

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

// get object that has the string that will be put in the table cell
 Task *task = [fetchedResultsController objectAtIndexPath:indexPath];

 //make button
 UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
 [button setTitle:@"Check" forState:UIControlStateNormal];
 [button setTitle:@"Checked" forState:UIControlStateHighlighted];

 //set the table cell text equal to the object's property
 cell.textLabel.text = [task taskName];

 //addbutton as a subview
 [cell.contentView addSubview:button];
 }

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

 static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

 // Configure the cell.
 [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

Little background, the table view is filled by a fetchedResultsController which fills the table view with Strings. Now I'm trying to add a button next to each String in each tableview cell. So far, I've been trying to create a button in my configureCell:atIndexPath method and then add that button as a subview to the table cell's content view, but for some reason the buttons do not show up. Below is the relevant code. If anyone wants more code posted, just ask and I'll put up whatever you want. Any help is greatly appreciated.

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

// get object that has the string that will be put in the table cell
 Task *task = [fetchedResultsController objectAtIndexPath:indexPath];

 //make button
 UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
 [button setTitle:@"Check" forState:UIControlStateNormal];
 [button setTitle:@"Checked" forState:UIControlStateHighlighted];

 //set the table cell text equal to the object's property
 cell.textLabel.text = [task taskName];

 //addbutton as a subview
 [cell.contentView addSubview:button];
 }

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

 static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

 // Configure the cell.
 [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

羞稚 2024-09-08 10:18:47

三条评论:

  1. 您可能需要设置 UIButton 的框架。否则它不知道它应该有多大以及应该放置在哪里。它可能带有默认框架,但我还没有调查过。
  2. 你的按钮漏了。调用 [UIButton buttonWithType:...]; 后,您不需要 retain
  3. 您可以向同一单元格添加多个按钮。如果您要重用单元格,则应首先调用 removeFromSuperview cell.contentView 的每个子视图。

Three comments:

  1. You'll probably need to set the frame of your UIButton. Otherwise it doesn't know how big it's supposed to be and where it's supposed to be placed. It's possible that it comes with a default frame, but I haven't investigated that.
  2. You're leaking your buttons. You don't need to retain after calling [UIButton buttonWithType:...];
  3. You have the possibility of adding multiple buttons to the same cell. If you're reusing a cell, then you should first invoke removeFromSuperview each subview of cell.contentView .
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文