我希望当行上的选项卡上出现一个包含两个按钮的 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
没有出现。如何解决?
最佳答案
尝试:
[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];
关于iphone - 在行选择处添加 uiview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9461487/