我想创建 UITableView,其中一些单元格是按钮。 帮帮我,正确的做法是什么?
1)我可以使用类似的东西:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
if (indexPath.section == 1){
if (indexPath.row == 0){
UIButton* loginButton = [[UIButton alloc] initWithFrame:CGRectMake(9,1,302, 48)];
[loginButton setBackgroundImage:[UIImage imageNamed:@"LoginButton.png"] forState:UIControlStateNormal];
[loginButton setBackgroundImage:[UIImage imageNamed:@"LoginButton_pressed.png"] forState:UIControlStateSelected];
[loginButton setTitle:@"Login" forState:UIControlStateNormal];
[cell setBackgroundColor:[UIColor clearColor]];
[cell addSubview:loginButton];
[loginButton release];
}
}
但我看到该按钮是透明的并且看起来很奇怪。
2)不要使用任何 UIButtons 并完全使用 UITableViewCells 制作我想要的内容
谢谢四位的关注,我对 iOS 完全陌生,但我正在尝试在没有 xib 的情况下制作应用程序。
最佳答案
您还没有设置按钮类型,它是只读属性。因此您不能直接分配它,因此请像这样更改您的代码:
UIButton* loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[loginButton setFrame:CGRectMake(5,5,302, 34)];
[loginButton setBackgroundImage:[UIImage imageNamed:@"LoginButton.png"] forState:UIControlStateNormal];
[loginButton setBackgroundImage:[UIImage imageNamed:@"LoginButton_pressed.png"] forState:UIControlStateSelected];
[loginButton setTitle:@"Login" forState:UIControlStateNormal];
[cell setBackgroundColor:[UIColor clearColor]];
[cell addSubview:loginButton];
关于ios - UITableView 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9927099/