简体   繁体   English

将按钮(UIButton)添加到使用drawRect绘制的自定义tableview单元格:

[英]Adding a button (UIButton) to a custom tableview cell drawn with drawRect:

I am currently working a on project where I have lots of custom table view cells. 我目前正在开展一个项目,我有很多自定义表视图单元格。 Part of the requirements is that the cells be able to expand if their default size can not hold all of the content. 部分要求是,如果单元格的默认大小无法容纳所有内容,则单元格可以展开。 If they need to be able to expand I have to add a UIButton to the cell and when it is tapped redraw it in a bigger view where all the data fits. 如果他们需要能够扩展,我必须向单元格添加一个UIButton,当它被点击时,在一个更大的视图中重绘它,所有数据都适合。 Currently in draw rect I essentially do this: 目前在绘制rect我基本上这样做:

if([self needsExpansion]) {
    [self addExpansionButton];
}

-(void)addExpansionButton {

    self.accessoryButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.accessoryButton setShowsTouchWhenHighlighted:YES];
UIImage *buttonImage = [UIImage imageNamed:@"blue_arrow_collaps_icon.png"];
[self.accessoryButton setFrame:CGRectMake(280, 82, buttonImage.size.width, buttonImage.size.height)];
[self.accessoryButton setImage:buttonImage forState:UIControlStateNormal];
[self.accessoryButton addTarget:self action:@selector(toggleExpanded) forControlEvents:UIControlEventTouchUpInside];    
[self addSubview:self.accessoryButton];
}

This works fine, except for when I click anywhere else in the cell the button flickers and disappears. 这工作正常,除了当我点击单元格中的任何其他位置时按钮闪烁并消失。 Anyone know how to propertly do this? 有谁知道如何做到这一点?

From the UITableViewCell Class Reference : 来自UITableViewCell类参考

You have two ways of extending the standard UITableViewCell object beyond the given styles. 您有两种方法可以将标准UITableViewCell对象扩展到给定样式之外。 To create cells with multiple, variously formatted and sized strings and images for content, you can get the cell's content view (through its contentView property) and add subviews to it. 要为内容创建具有多个不同格式和大小的字符串和图像的单元格,您可以获取单元格的内容视图(通过其contentView属性)并向其添加子视图。

Instead of adding the accessory button as a subview of the UITableViewCell, you should add it as a subview of the contentView: 您应该将其添加为contentView的子视图,而不是将附件按钮添加为UITableViewCell的子视图:

[[self contentView] addSubview:self.accessoryButton];

Have you worked out the following problem in your design approach?: Let's say one of your cells (let's call it A) determines it needs expansion, so you add a button to it. 您是否在设计方法中解决了以下问题?:让我们说您的一个单元格(让我们称之为A)确定它需要扩展,因此您需要添加一个按钮。 What happens when the user scrolls through the UITableView? 当用户滚动UITableView时会发生什么? For performance reasons, your UITableView delegate should be using dequeueReusableCellWithIdentifier in tableView:cellForRowAtIndexPath:. 出于性能原因,您的UITableView委托应该在tableView中使用dequeueReusableCellWithIdentifier:cellForRowAtIndexPath:。 So you'll be reusing A to display a different row of the table. 因此,您将重复使用A来显示表格的不同行。 Do you really want A to have an accessory button? 你真的希望A有配件按钮吗? Probably not, since it's now representing a different object. 可能不是,因为它现在代表了一个不同的对象。

You're probably better off doing the cell customization at the UITextViewDelegate. 您可能最好在UITextViewDelegate上进行单元格自定义。 In tableView:cellForRowAtIndexPath:, you can determine if the object being displayed at the row specified needs a button, and if it does add it as a subview to the contentView. 在tableView:cellForRowAtIndexPath:中,您可以确定在指定行显示的对象是否需要按钮,以及是否将其作为子视图添加到contentView。

Then again, if your table size is always relatively small (say < 50), you can use this approach Jeremy Hunt Schoenherr suggests. 再说一次,如果你的桌子大小总是相对较小(比如<50),你可以使用这种方法,Jeremy Hunt Schoenherr建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM