简体   繁体   English

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

[英]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.

相关问题 如何保持UITableView单元格处于选中状态 - How to keep a UITableView cell selected 通过dequeueReusableCellWithIdentifier重用时,UITableViewCell中的UISegmentedControl会更改段索引顺序 - UISegmentedControl in UITableViewCell changing segment index order when reused through dequeueReusableCellWithIdentifier 如何使 UISegmentedControl 的选定部分变暗? - How do you make the selected segment of a UISegmentedControl darker? UISegmentedControl:使用自定义图像时如何不突出显示已选择的段 - UISegmentedControl: how not to highlight already selected segment when using custom images 如何使UISegmentedControl的选定段不可选择? - How to make UISegmentedControl's selected segment unselectable? 如何在 Swift 中更改 UISegmentedControl 的选定段 tintColor - How to change selected segment tintColor of UISegmentedControl in Swift 如何以编程方式为 UISegmentedControl 中的段设置选定状态 - How to programatically set the selected state for a segment in UISegmentedControl 如何使用UISegmentedControl检查选择了哪个段 - How to check which segment is selected with UISegmentedControl 如何为 UISegmentedControl 的特定部分设置可访问性标签? - How do I set the accessibility label for a particular segment of a UISegmentedControl? 如何让我的段UISegmentedControl响应? - How do I get my segment UISegmentedControl to respond?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM