I am a newbie so any suggestions for the following problem I am having would be appreciated.
I have an app where y开发者_如何学编程ou enter a number of items and then in a subsequent tableview, they will all load and I can assign each item a value using a uislider.
I know how to track and save the values if I know the exact number of items I will have but given that the number of uislider values I have to track depends on the number of items entered, I am not sure where to go.
So far in my sliderValueChanged method, I have the following which allows me to track each uislider subview:
- (IBAction)sliderChangedValues:(id)sender {
UISlider *slider = (UISlider *)sender;
CGPoint correctedPoint =
[slider convertPoint:slider.bounds.origin toView:self.tableView];
NSIndexPath *indexPath =
[self.tableView indexPathForRowAtPoint:correctedPoint];
NSLog(@"Using slider in row %d", indexPath.row);
}
Here is my cellForRowAtIndexPath method for loading the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
Criteria *myCriteria = [criteriasRankingArray objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"CriteriaRankingCell";
OptionsDetailViewCell *cell = (OptionsDetailViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[self.cellNib instantiateWithOwner:self options:nil];
cell = optionsCell;
self.optionsCell = nil;
}
cell.criteriaRankSlider.minimumValue = 1.00;
cell.criteriaRankSlider.maximumValue = [criteriasRankingArray count];
cell.criteriaRankSlider.continuous = YES;
cell.criteriaNameLabel.text = [NSString stringWithFormat:@"%@", myCriteria.criteriaName];
cell.criteriaRankSlider.value = [myCriteria.criteriaRank floatValue];
return cell;
}
How do I go about tracking each value and then where would I set each sliders value and save to context?
You're nearly there. In your sliderChangedValues
method, add the following:
Criteria *myCriteria = [criteriasRankingArray objectAtIndex:indexPath.row];
myCriteria.criteriaRank = [NSNumber numberWithFloat:slider.value];
I wouldn't save the context in this method as you've got the slider set to continuous, so that could trigger lots of save messages. Save it when the user leaves the view or some other suitable point.
精彩评论