Reputation: 650
My main problem is keeping the selected value of a UISegmentedControl when the cell that it is inside is reused. 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. When I select a segment in row 3, the row that shows up when "3" disappears has the same selected segment. I would like to make sure that the only rows with selected segments are those that the user actually changes.
Upvotes: 1
Views: 1359
Reputation: 8170
Your model for each row should have a property that keeps the selected segment, lets call it 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.
Then in your cellForRowAtIndexPath:
method, you update the UISegmentedControl's selected index with the value of the selectedSegment
property.
Upvotes: 3
Reputation: 793
Just keep selected indexes of segmented control in mutable array as NSNumber objects.
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
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)
P.S Initially you need to create indexes array with default values
Upvotes: 0