UITableView 单元格中的 UIButton,例如“删除事件"

UIButton in UITableView cell like quot;Delete Eventquot;(UITableView 单元格中的 UIButton,例如“删除事件)
本文介绍了UITableView 单元格中的 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),您将创建 another 新按钮,将其添加到单元格(在旧按钮之上)等等.事实是它已经通过 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.

按钮需要有一个位置和一个目标设置,以便在点击时它会做一些事情.如果每个单元格都有这个按钮,一个巧妙的技巧是让它们都指向同一个目标方法,但将按钮的 tag 值设置为单元(在单元分配块之外,因为它因每个单元而异).按钮的常见点击处理程序将使用发送者的标记值来查找数据源列表中的基础数据.

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 后,在按钮上调用release.这样,保留计数将降至零,并且当父对象被释放时,对象实际上会被释放.

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.

您可以将其作为单元格的 accessoryView 返回,而不是通过 addSubview 添加按钮,这样您就不必担心定位它(除非您已经将附件视图用于其他用途——例如显示按钮).

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

这篇关于UITableView 单元格中的 UIButton,例如“删除事件"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
iOS convert audio sample rate from 16 kHz to 8 kHz(IOS将音频采样率从16 kHz转换为8 kHz)
Enforcing an audio sampling rate in iOS(在iOS中强制音频采样率)
HTTPS request using volley(使用 volley 的 HTTPS 请求)