我想在我的 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/