将按钮添加到UITableViewCell的附件视图 [英] Add button to UITableViewCell's Accessory View

查看:61
本文介绍了将按钮添加到UITableViewCell的附件视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:当用户选择一个单元格时,会向该单元格添加一个按钮.在我的didSelectRowAtIndexPath函数中,我具有以下内容:

Goal: when a user selects a cell, a button is added to that cell. Within my didSelectRowAtIndexPath function I have the following:

UIButton *downloadButton = [[UIButton alloc] init];
downloadButton.titleLabel.text = @"Download";
[downloadButton setFrame:CGRectMake(40, 0, 100, 20)];
[[self.tableView cellForRowAtIndexPath:indexPath].accessoryView addSubview:downloadButton];
[[self.tableView cellForRowAtIndexPath:indexPath].accessoryView setNeedsLayout];

[downloadButton release];

不幸的是,这似乎没有任何作用.我要重新绘制单元格校正吗?我需要以其他方式添加它吗?

Unfortunately that doesn't seem to do anything. Am I redrawing the cell correction? Do I need to add it another way?

推荐答案

尝试以下代码块,而不是上面提供的代码块:

Try this block of code instead of the block you provided above:

UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[downloadButton setTitle:@"Download" forState:UIControlStateNormal];
[downloadButton setFrame:CGRectMake(0, 0, 100, 35)];
[tableView cellForRowAtIndexPath:indexPath].accessoryView = downloadButton;

这应该显示按钮,但是您仍然需要使用addTarget将某种选择器连接到该按钮. (我不确定在这种情况下,是否侦听annexingButtonTappedForRowWithIndexPath委托是否可以正常工作,请先尝试该操作,看看它是否在您按下按钮时触发.)

This should display the button, but you will still need to hook up some kind of selector to it using addTarget. (I am not sure if listening in for the accessoryButtonTappedForRowWithIndexPath delegate will work in this case, try that first and see if it fires on your button press.)

这篇关于将按钮添加到UITableViewCell的附件视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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