我想当行上的标签时,包含两个按钮的UIView
出现在单元格的中心,所以我做了以下代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//how can I get the text of the cell here?
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIView *v = [[UIView alloc] init];
v.center = cell.center;
UIButton*button1 =[[UIButton alloc] init];
button1.frame = CGRectMake(v.center.x - 5 , v.center.y , 5 , v.center.y + 4 );
button1.titleLabel.text = @"I'm label 1";
[v addSubview: button1];
UIButton*button2 =[[UIButton alloc] init];
button2.frame = CGRectMake(v.center.x - + 1 , v.center.y , 5 , v.center.y + 4 );
button2.titleLabel.text = @"I'm label 2";
[v addSubview: button2];
[cell addSubview:v];
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
UIView
没有出现。怎么解决?
答案 0 :(得分:2)
尝试:
[cell.contentView addSubview: v];
它应该有用。
编辑: 试试这个代码。相应地给出框架,它会起作用。
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(44, 100, 200, 200)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 50, 50);
[v addSubview:button];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:v];
答案 1 :(得分:1)
UIView *v = [[UIView alloc] init];//You have not set the frame of the v;
答案 2 :(得分:0)
initWithFrame
是视图的指定初始化程序。
由于您使用init
,因此无法知道要创建的视图的大小。
UIButton *button1 = [[UIButton alloc] init];
也不正确。 UIButtons是使用buttonWithType:
类方法创建的。