【问题标题】:iOS Segmented Control Set Segments After CreationiOS 分段控件创建后设置分段
【发布时间】:2014-06-10 15:59:05
【问题描述】:

在自定义的 uitableviewcell 中,分段控件是在创建行时创建的。每一行都是表格中的一个问题。根据问题的类型,分段控件可以有不同的答案。在 UISegmentedControl 参考中,它只列出了允许一次设置所有段的 init 方法。有没有比使用 remove 和 insert 更新段以获得相关段更好的方法?

在自定义 UITableView 单元格的 init 中

_answerSegmented = [[UISegmentedControl alloc] init];
[_answerSegmented addTarget:self action:@selector(answerChanged:) forControlEvents:UIControlEventValueChanged];
_answerSegmented.backgroundColor = [UIColor columnHeaderBackground];
[self addSubview:_answerSegmented];

直到后来它才知道段应该是什么

NSMutableArray *answers = [NSMutableArray arrayWithObjects:@"IN", @"OUT", nil];
if (_question.noItem.boolValue) {
    [answers addObject:@"N/O"];
}
if (_question.naItem.boolValue) {
    [answers addObject:@"N/A"];
}
_answerSegmented.segments = answers; // <---- this line gives a compile error

【问题讨论】:

    标签: objective-c uitableview uisegmentedcontrol segments


    【解决方案1】:

    不,没有更好的方法。如果所有分段控件的段数相同,则创建具有该段数的控件。然后只需使用setTitle:forSegmentAtIndex: 更改每个段的标题,一次一个。

    如果数字可以更改,请使用removeAllSegmentsinsertSegmentWithTitle:atIndex:animated:

    或者只是创建一个新的分段控件并每次删除旧的。

    【讨论】:

    • 谢谢。项目的数量是动态的,因此必须删除并重新插入。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多