繁体   English   中英

当重用单元格时,如何将UISegmentedControl的选定段保留在UITableView中?

[英]How do I keep the selected segment of UISegmentedControl inside UITableView when cell is reused?

我的主要问题是当UISegmentedControl内部的单元格被重用时,保留UISegmentedControl的选定值。 当我滚动时,重用的单元格仍然具有相同的分段控件值。

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

编辑以澄清:我想保留每行的选择。 例如,我可能有10个可见行和30行。 当我在第3行中选择一个段时,“3”消失时显示的行具有相同的选定段。 我想确保所选段的唯一行是用户实际更改的行。

每行的模型都应该有一个保留所选段的属性,让我们称之为selectedSegment 当用户单击某个段时,会影响表示受影响行的对象实例的selectedSegment属性的值。

然后在您的cellForRowAtIndexPath:方法中,使用selectedSegment属性的值更新UISegmentedControl的选定索引。

只需将可变数组中的分段控件的选定索引保留为NSNumber对象。

- (UITableViewCell *)tableView:(UITableView *)thisTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法将分段控件的tag属性设置为indexPath.row并通过indexPath.row从数组中获取选定值

当分段控件更改所选索引时,您需要将数组中的选定索引替换为新值(通过数组中的标记值获取所选索引值)

PS最初,您需要使用默认值创建索引数组

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM