我有一個自定義的UITableViewCell與它一個UISegmentedControl如下: #進口如何在單元重新加載時保持UITableViewCell中UISegmentedControl的選定索引?
@interface TFSegmentedControlCell : UITableViewCell {
UISegmentedControl *segmentedControl;
UILabel *questionTitle;
}
@property (nonatomic, retain) UISegmentedControl *segmentedControl;
@property (nonatomic, retain) UILabel *questionTitle;
@end
我重用這些細胞良好的內存管理,我可以設想在那裏我將有100個左右的細胞的情況。段標題取自從在線加載的數組。我這樣做如下:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
TFSegmentedControlCell *cell = (TFSegmentedControlCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TFSegmentedControlCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSArray *segmentHeaders = [[[self.questionsArray objectAtIndex:indexPath.row] valueForKey:@"answer_labels"] componentsSeparatedByString:@","];
for (int i=0; i<[segmentHeaders count]; i++) {
[cell.segmentedControl insertSegmentWithTitle:[segmentHeaders objectAtIndex:i] atIndex:cell.segmentedControl.numberOfSegments animated:NO];
}
cell.questionTitle.text = [NSString stringWithFormat:@"%u. %@",indexPath.row + 1,[[self.questionsArray objectAtIndex:indexPath.row] valueForKey:@"description"]];
return cell;
}
問題是,所選擇的指數保持與細胞不是指數,也就是說,如果我向下滾動並加載它(從關閉屏幕)的新細胞,就會有選擇第一個單元格的索引(現在在屏幕外)。如果我在新加載的單元格中選擇不同的段,那麼當向後滾動時,它將更改第一個單元格的選定段。
當一個段被選中時,我目前沒有調用任何方法,只需按下'Done'按鈕迭代所有UISegmentedControls並將所有selectedSegments添加到一個數組並將其發送到在線php文件進行處理。
思想非常感謝:)
解決方案:
我添加int row
到TFSegmentedControlCell.h
然後我添加用於UISegmentedControl值變化時的方法:
-(void)selectedTFSegment:(id)sender {
TFSegmentedControlCell *cell = (TFSegmentedControlCell *)[[sender superview] superview];
[[self.questionsArray objectAtIndex:cell.row] setValue:[NSString stringWithFormat:@"%i",cell.segmentedControl.selectedSegmentIndex] forKey:@"selectedSegment"];
}
然後重新配置細胞的itialisation:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
int row = indexPath.row;
static NSString *CellIdentifier = @"Cell";
TFSegmentedControlCell *cell = (TFSegmentedControlCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TFSegmentedControlCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSArray *segmentHeaders = [[[self.questionsArray objectAtIndex:row] valueForKey:@"answer_labels"] componentsSeparatedByString:@","];
[cell.segmentedControl removeAllSegments];
for (int i=0; i<[segmentHeaders count]; i++) {
[cell.segmentedControl insertSegmentWithTitle:[segmentHeaders objectAtIndex:i] atIndex:cell.segmentedControl.numberOfSegments animated:NO];
}
cell.row = row;
[cell.segmentedControl addTarget:self action:@selector(selectedTFSegment:) forControlEvents:UIControlEventValueChanged];
if ([[self.questionsArray objectAtIndex:row] objectForKey:@"selectedSegment"] != nil) {
int newIndex = [[[self.questionsArray objectAtIndex:row] objectForKey:@"selectedSegment"] intValue];
[cell.segmentedControl setSelectedSegmentIndex:newIndex];
} else {
[cell.segmentedControl setSelectedSegmentIndex:UISegmentedControlNoSegment];
}
cell.questionTitle.text = [NSString stringWithFormat:@"%u. %@",indexPath.row + 1,[[self.questionsArray objectAtIndex:row] valueForKey:@"description"]];
return cell;
}
我想這可能是這樣做的唯一途徑,但我不打電話從UISegmentedControl任何選擇計劃。 如果我這樣做,我需要調用一個方法,它需要確定哪個單元格的UISegmentedControl已經改變了值。 我會研究它,但理想情況下我可以避免這種方式! – Tim
採取了暴跌,只是做到了。感謝您的回答。我在這個問題中包含了我的重做代碼。 – Tim