【问题标题】:iOS how to add radio buttonsiOS如何添加单选按钮
【发布时间】:2014-05-12 10:30:28
【问题描述】:

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

【问题讨论】:

  • 我建议你创建一个 UITableViewController(或者你的自定义类从 UITableViewController 继承),创建一个带有基本 UILabel 和 UISwitch 的 UITableViewCell,就是这样。进行插座连接,瞧 :)。这是一个老例子,但你有这个想法:stackoverflow.com/questions/4585840/…

标签: ios uitableview uibutton


【解决方案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;
}

【讨论】:

  • 如果我使用 CustomCell 的类是 UIViewController,我有 UITableview,当使用 [_tableView register...] 时,我收到错误消息 no visible interface UITableView 声明选择器寄存器。如何解决?
【解决方案2】:

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

然后设置您想要的表格:

【讨论】:

    猜你喜欢
    • 2012-09-26
    • 2012-05-20
    • 1970-01-01
    • 2018-04-19
    • 2011-05-31
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    相关资源
    最近更新 更多