Mar*_*tör 8 iphone uibutton uitableview
我想在表格单元格中添加一个按钮.日历应用程序中的"删除事件"启发了我...(类似案例是联系人中的"共享联系人")
截至目前已有
- (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];
Run Code Online (Sandbox Code Playgroud)
确实产生了一个按钮.它看起来还不怎么样(很明显我从未处理过iPhone中的按钮),但这至少是正确的方法吗?
Ram*_*min 19
现在,每次显示一个单元格时,您都可以分配一个按钮,设置其值,并将其添加到单元格的contentView中.当单元格被重用(通过dequeueReusableCellWithIdentifier
)时,你将创建另一个新按钮,将它添加到单元格中(在旧单元格之上)等.事实上它已经通过addSubview
但没有明确的释放意味着每个按钮的保留计数永远不会零,所以他们都会坚持下去.经过一段时间的上下滚动,单元格最终会有数百个按钮子视图,这可能不是你想要的.
一些提示:
永远不要在cellForRowAtIndexPath
调用中分配内容,除非在dequeueReusableCellWithIdentifier
返回nil并且您正在初始化单元格时完成.在以后的所有其他时间,您将被移交给您已经设置的缓存单元格,因此您只需更改标签或图标即可.您将要if
在单元格分配代码之后在条件权限内移动所有按钮分配内容.
按钮需要有一个位置,也需要设置一个目标,以便在点击时执行某些操作.如果每个单元格都有这个按钮,一个巧妙的技巧就是让它们都指向相同的目标方法,但是将按钮的tag
值设置indexPath.row
为单元格的值(在单元格分配块之外,因为它对于每个单元格而变化).按钮的公共点击处理程序将使用发送方的标记值来查找dataSource列表中的基础数据.
release
完成后调用按钮addSubview
.这样,保留计数将降为零,并且在释放父对象时实际上将释放对象.
addSubview
您可以将其作为accessoryView
单元格返回,而不是添加按钮,因此您不必担心定位它(除非您已经将accessoryView用于其他内容 - 例如公开按钮).
我采用了不同的方法来创建日历应用程序中"删除事件"按钮的等效项.我没有添加按钮作为子视图,而是向单元格添加了两个背景视图(红色和深红色,具有漂亮的渐变),然后对角进行四舍五入并将边框设置为灰色.
下面的代码创建了一个可重用的单元格(以通常的方式).引用的两个图像('redUp.png'和'redDown.png')取自日历的"删除事件"按钮的屏幕截图.(这似乎比以编程方式创建渐变更快.)可以进行更精细的调整以使其更接近Calendar的"删除事件"外观,但这非常接近.
按钮的动作由tableView委托方法tableView:didSelectRowAtIndexPath:方法触发.
// create a button from a table row like the Calendar's 'Delete Event' button
// remember to have an #import <QuartzCore/QuartzCore.h> some above this code
static NSString *CellWithButtonIdentifier = @"CellWithButton";
UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:CellWithButtonIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellWithButtonIdentifier] autorelease];
[[cell textLabel] setTextAlignment: UITextAlignmentCenter];
UIImageView* upImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"redUp.png"]];
UIImageView* downImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"redDown.png"]];
[cell setBackgroundView: upImage];
[cell setSelectedBackgroundView: downImage];
[[upImage layer] setCornerRadius:8.0f];
[[upImage layer] setMasksToBounds:YES];
[[upImage layer] setBorderWidth:1.0f];
[[upImage layer] setBorderColor: [[UIColor grayColor] CGColor]];
[[downImage layer] setCornerRadius:8.0f];
[[downImage layer] setMasksToBounds:YES];
[[downImage layer] setBorderWidth:1.0f];
[[downImage layer] setBorderColor: [[UIColor grayColor] CGColor]];
[[cell textLabel] setTextColor: [UIColor whiteColor]];
[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundColor:[UIColor clearColor]]; // needed for 3.2 (not needed for later iOS versions)
[[cell textLabel] setFont:[UIFont boldSystemFontOfSize:20.0]];
[upImage release];
[downImage release];
}
return cell;
Run Code Online (Sandbox Code Playgroud)
是的,这通常是正确的方法。
一个提示:
设置按钮事件的回调,以便它在单击时实际执行某些操作。
[myButton addTarget:self action:@selector(whatMyButtonShouldDo:)
forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)编辑:调整了使用系统按钮的代码,这使得我编写的许多内容变得不必要。
归档时间: |
|
查看次数: |
22813 次 |
最近记录: |