UITutView单元格中的UIButton,如“删除事件” [英] UIButton in UITableView cell like "Delete Event"

查看:125
本文介绍了UITutView单元格中的UIButton,如“删除事件”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在表格单元格中添加一个按钮。日历应用程序中的删除事件启发了我...(类似案例是联系人中的共享联系人)

I'd like to add a button to a table cell. The "Delete Event" in the calendar app inspired me... (a similar case is "Share Contact" in contacts)

截至目前已有

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  //..yadayadayada
  cell = [tableView dequeueReusableCellWithIdentifier:@"buttonCell"];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"buttonCell"] autorelease];
  }
  UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
  [button setBackgroundColor:[UIColor redColor]];
  button.titleLabel.text = @"Foo Bar";
  [cell.contentView addSubview:button];

确实产生一个按钮。它看起来还不怎么样(很明显我从未处理过iPhone中的按钮),但这至少是正确的做法吗?

which produces a button, indeed. It doesn't look yet how it's supposed to (it's obvious I've never dealt with buttons in iPhone, yet), but is this at least the right approach?

推荐答案

现在,每次显示一个单元格时,您都可以分配按钮,设置其值,并将其添加到单元格的contentView中。当单元格被重用时(通过 dequeueReusableCellWithIdentifier ),你将创建另一个新按钮,将其添加到单元格中(在旧单元格之上)事实上,它已经通过 addSubview ,但没有明确的释放意味着每个按钮的保留计数永远不会变为零,所以他们都会坚持下去。在上下滚动一段时间之后,单元格最终会有数百个按钮子视图,这可能不是您想要的。

The way you have it now each time a cell is shown you're allocating a button, setting its value, and adding it to the cell's contentView. When the cell gets reused (via dequeueReusableCellWithIdentifier) you'll be creating another new button, adding it to the cell (on top of the old one) etc. The fact that it's gone through addSubview but no explicit release means each button's retain count will never go to zero so they'll all stick around. After a while of scrolling up and down the cell will end up with hundreds of button subviews which probably isn't what you want.

一些提示:


  • 永远不要在 cellForRowAtIndexPath 调用中分配内容,除非在 dequeueReusableCellWithIdentifier 返回nil并且您正在初始化单元格。所有其他后续时间您将被移交给您已经设置的缓存单元格,因此您只需更改标签或图标即可。您将要在单元格分配代码之后立即在 if 条件内移动所有按钮分配内容。

  • Never allocate stuff inside a cellForRowAtIndexPath call unless it's done when dequeueReusableCellWithIdentifier is returning nil and you're initializing the cell. All other subsequent times you'll be handed back the cached cell that you will have already set up so all you have to do is change the labels or icons. You're going to want to move all that button allocation stuff up inside the if conditional right after the cell allocation code.

按钮需要有一个位置,也需要设置一个目标,以便在点击时执行某些操作。如果每个单元格都有这个按钮,一个巧妙的技巧是让它们都指向相同的目标方法,但将按钮的标记值设置为单元格的indexPath.row (在单元分配块之外,因为它对于每个单元格而变化)。按钮的公共点按处理程序将使用发件人的标记值来查找dataSource列表中的基础数据。

The button needs to have a position and also a target set for it so it'll do something when tapped. If every cell is going to have this button a neat trick is to have them all point to the same target method but set the button's tag value to the indexPath.row of the cell (outside the cell allocation block since it varies for each cell). The common tap handler for the button would use the tag value of the sender to look up the underlying data in the dataSource list.

调用 addSubview 后,在按钮上发布。这样,保留计数将降至零,并且当父项被释放时,对象实际上将被释放。

Call release on the button after you've done an addSubview. That way the retain count will fall to zero and the object will actually get released when the parent is released.

而不是通过<$ c $添加按钮c> addSubview ,您可以将其作为单元格的 accessoryView 返回,这样您就不必担心定位它(除非您是已经将accessoryView用于其他东西 - 比如披露按钮。)

Instead of adding the button via addSubview, you can return it as the accessoryView for the cell so you don't have to worry about positioning it (unless you're already using the accessoryView for something else -- like disclosure buttons).

这篇关于UITutView单元格中的UIButton,如“删除事件”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆