[英]How do I keep the selected segment of UISegmentedControl inside UITableView when cell is reused?
My main problem is keeping the selected value of a UISegmentedControl when the cell that it is inside is reused. 我的主要问题是当UISegmentedControl内部的单元格被重用时,保留UISegmentedControl的选定值。 When I scroll, the reused cell still has the same value for the segmented control.
当我滚动时,重用的单元格仍然具有相同的分段控件值。
- (UITableViewCell *)tableView:(UITableView *)thisTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"yesNoCell";
testCell = [thisTableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
testCell.mainText.text = [ questionArray objectAtIndex:indexPath.row];
[testCell.mainControl setFrame:CGRectMake(690, 22, 334, 40)];
return testCell;
}
Edit for clarification: I want to keep the selection for each row. 编辑以澄清:我想保留每行的选择。 I may have, for example, 10 visible rows and 30 rows in all.
例如,我可能有10个可见行和30行。 When I select a segment in row 3, the row that shows up when "3" disappears has the same selected segment.
当我在第3行中选择一个段时,“3”消失时显示的行具有相同的选定段。 I would like to make sure that the only rows with selected segments are those that the user actually changes.
我想确保所选段的唯一行是用户实际更改的行。
Your model for each row should have a property that keeps the selected segment, lets call it selectedSegment
. 每行的模型都应该有一个保留所选段的属性,让我们称之为
selectedSegment
。 When the user clicks on a segment you affect the value of the selectedSegment
property for the instance of the object representing the affected row. 当用户单击某个段时,会影响表示受影响行的对象实例的
selectedSegment
属性的值。
Then in your cellForRowAtIndexPath:
method, you update the UISegmentedControl's selected index with the value of the selectedSegment
property. 然后在您的
cellForRowAtIndexPath:
方法中,使用selectedSegment
属性的值更新UISegmentedControl的选定索引。
Just keep selected indexes of segmented control in mutable array as NSNumber objects. 只需将可变数组中的分段控件的选定索引保留为NSNumber对象。
In - (UITableViewCell *)tableView:(UITableView *)thisTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method set tag property of segmented control to indexPath.row and get selected value from array by indexPath.row 在
- (UITableViewCell *)tableView:(UITableView *)thisTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法将分段控件的tag属性设置为indexPath.row并通过indexPath.row从数组中获取选定值
When segmented control changed selected index you need to replace selected index in array to new value(get the selected index value by tag value from array) 当分段控件更改所选索引时,您需要将数组中的选定索引替换为新值(通过数组中的标记值获取所选索引值)
PS Initially you need to create indexes array with default values PS最初,您需要使用默认值创建索引数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.