ios - UITableView 中的按钮

标签 ios uitableview uibutton

我想创建 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/

相关文章:

iOS - 推送内容

ios - 使用谷歌地图快速获取我的位置

iphone - 更新应用程序时出现代码签名错误

iphone - UIButton 反复按下

ios - UIButton 上的 AddTarget 未触发

iphone - 对 NSArray 的 XML 响应

ios - 如何根据点击的单元格更改 View Controller 的数据

ios - UITableViewCell 中的 UILabel 高度变化仅发生在第二次滚动中

Swift Firebase : UIRefreshControl with . 子添加

ios - UIButton 图像在模拟器上工作但不在设备上