2011-03-23 51 views
1

我尝试在不使用Interface Builder的情况下在tableview单元格内创建一个按钮。 我发现这个代码,我认为这是一个用于创建一个按钮:在没有Interface Builder的情况下在tableview单元格内创建按钮

- (void)viewDidLoad { 
[super viewDidLoad]; 

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.frame = CGRectMake(270,10,30,30); 
[button setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]; 
button.backgroundColor = [UIColor clearColor]; 

} 

但是当我尝试运行该程序没有在模拟器上显示出来。 我错过了用这段代码创建按钮的东西吗?

回答

1

您需要将按钮添加到细胞 - 你试过

[cell.contentView addSubview:button] 

调整您的按钮的框架以将其放置在contentView中并正确调整按钮的大小。

+0

谢谢你的回答。我尝试了你建议的代码,但没有奏效。 – asadawut 2011-03-23 23:49:15

+0

它应该。你有没有尝试过让一切都变得非常简单,只需要在单元格中显示一个按钮,如下所示:http://stackoverflow.com/q/2633603/189804 – 2011-03-24 00:09:56

0

你必须在这个方法中添加代码,但首先是添加自定义单元格的表视图,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

forwordBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [forwordBtn setTitle:@"Forward" forState:UIControlStateNormal]; 

    [forwordBtn addTarget:self 
        action:@selector(forwordButtonPressed) 
     forControlEvents:UIControlEventTouchUpInside]; 
    forwordBtn.frame = CGRectMake(0, 0, 10, 10); 

    [cell.contentview addSubview:forwordBtn]; 
} 
0

你应该写在cellforRow所有代码索引数据源的方法和添加对细胞的按钮你可以使用这行代码

[cell.contentView addsubview:buttonobject];

button.frame = CGRectMake(10,10,30,30);

还改变游览按钮的背景或风格为您的构象它会在那里或没有。

如果它的工作通知我们,它就会生效。

您的欢迎。

相关问题