iOS如何添加单选按钮

时间:2014-04-01 14:51:29

标签: 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并为此定义听筒和页脚?有人可以提供一些如何开始和使用它的例子吗?

2 个答案:

答案 0 :(得分:1)

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

使用表格视图和单元格标识符注册自定义单元格类,然后像对待任何其他单元格一样自定义它。

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;
}

答案 1 :(得分:0)

最简单的解决方案是在界面构建器中使用静态布局设置UITableViewController。

enter image description here

然后设置你想要它的表:

enter image description here