iOS如何添加单选按钮

标签 ios uitableview uibutton

I would like to add in my ViewController something that should look identical with the first item of this picture (Simple passcode + radio button). Do I need to use a UITableView and define the hearder and the footer for this? Can someone provide me some examples of how to start and work with this?

我想在我的 ViewController 中添加一些看起来应该与这张图片的第一项相同的东西(简单密码 + 单选按钮)。我是否需要使用 UITableView 并为此定义听众和页脚?有人可以为我提供一些如何开始和使用它的例子吗?

最佳答案

您需要一个自定义的 UITableViewCell,单元格左侧有一个 UILabel,右侧有一个 UISwitch。

使用 TableView 和单元格标识符注册自定义单元格类,然后像自定义任何其他单元格一样对其进行自定义。

static NSString *const CellIdentifier = @"customCell";

/**
 *  Called after the controller’s view is loaded into memory.
 */
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerClass:[CustomCell class] forCellIdentifier:CellIdentifier];
}

/**
 *  Asks the data source for a cell to insert in a particular location of the table view.
 *
 *  @param  tableView                   A table-view object requesting the cell.
 *  @param  indexPath                   An index path locating a row in tableView.
 *
 *  @return An object inheriting from UITableViewCell that the table view can use for the specified row.
 */
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.textLabel.text = @"Simple Passcode";

    return cell;
}

关于iOS如何添加单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22789455/

相关文章:

ios - 解析iOS SDK : Refresh PFQueryTableViewController upon new object

ios - 当我想使用 tableview.deleteRows(at : , with: ) 时应用程序崩溃

ios - 重新限制 UIButton 的宽度

swift - 在 Swift (tvOS) 中,如何更改 UIButton 的突出显示和焦点颜色?

iOS Xcode : UIButton titleLabel color keeps changing

ios - 另一个具有非透明内容的透明 UIView 上的透明 View ?

javascript - 源代码和 DOM 有什么区别?

iphone - 降级 iOS/升级到不是最新的版本

ios - 单例中 getter 和 setter 的线程安全

ios - 创建嵌套 TableView 或单独的页面?