如何将 UIButton 放入 uitableview 单元格?
Posted
技术标签:
【中文标题】如何将 UIButton 放入 uitableview 单元格?【英文标题】:How to place UIButton into the uitableview cell? 【发布时间】:2011-05-26 04:14:37 【问题描述】:我可以使用textlabel
和detaillabeltext
创建一个tableview
。
除此之外,是否可以在单元格中添加UIButton
。
我的意思是在detaillabeltext之后,我可以放置一个UIButton
(比如“删除”按钮)
目前我有以下代码
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//-----------------------------------------------------------------------------------------------------
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
profileName = [appDelegate.sentItemsList objectAtIndex:indexPath.row];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString *subjectData = [profileName.sent_subject stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
cell.textLabel.text = [NSString stringWithFormat: @"%@ ", subjectData];
cell.textLabel.font = [UIFont boldSystemFontOfSize:13];
NSString *CompanyName = [profileName.sent_content stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
cell.detailTextLabel.text = [NSString stringWithFormat: @"%@ ",CompanyName];
cell.detailTextLabel.font = [UIFont systemFontOfSize:10];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.backgroundColor = [UIColor colorWithRed:230.0/255.0 green:249.0/255.0 blue:230.0/255.0 alpha:2.0];
return cell;
感谢您的宝贵时间和帮助!
【问题讨论】:
UIButton in UITableView cell like "Delete Event"的可能重复 添加按钮是为了什么?就像你想实现删除功能一样,你可以使用附件,否则你必须将每件事都实现为表格视图单元格的子视图。 @rptwsthi:不像删除功能。我想调用我的“添加到收藏夹”按钮 【参考方案1】:UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[cell addSubview:button];
【讨论】:
这是一个正确的解决方案,但它会隐藏所有先前的文本(标签和子标签) 不,它不会隐藏以前的...正确给出坐标 @rptwsthi: 不然看这个教程就好了e-string.com/content/custom-uitableviewcells-interface-builder 从本教程中使用也可以在您的单元格中添加图像blog.webscale.co.in/?p=284【参考方案2】:您可以在单元格的内容视图中添加按钮或任何其他控件。
UIButton *cellButton = [[UIButton alloc]init];
[cellButton addTarget:self action:@selector(uploadData:)forControlEvents:UIControlEventTouchDown];
[cellButton setTitle:@"Upload Now" forState:UIControlStateNormal];
cellButton.frame = CGRectMake(100, 180, 150, 35);
[cell.contentview addSubview:cellButton];
[cellButton release];
向单元格的内容视图添加任何控件
【讨论】:
以上是关于如何将 UIButton 放入 uitableview 单元格?的主要内容,如果未能解决你的问题,请参考以下文章