2011-06-23 30 views
0

我有一個自定義的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 rowTFSegmentedControlCell.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; 
} 

回答

1

每當你的tableView需要一個單元格時,它會調用tableView:cellForRowAtIndexPath:。因此,在該方法的某處(例如,您分配問題標題的位置),您需要設置當前單元格的分段控件的選定索引。理想情況下,你可以做這樣的事情:

[cell.segmentedControl setSelectedSegmentIndex: 
    [[self.questionsArray objectAtIndex:indexPath.row] valueForKey:@"selectedIndex"]]; 

這意味着在questionsArray你的對象需要一個NSInteger的財產selectedIndex(例如),這將反映各行所選擇的片段指數值。

+0

我想這可能是這樣做的唯一途徑,但我不打電話從UISegmentedControl任何選擇計劃。 如果我這樣做,我需要調用一個方法,它需要確定哪個單元格的UISegmentedControl已經改變了值。 我會研究它,但理想情況下我可以避免這種方式! – Tim

+0

採取了暴跌,只是做到了。感謝您的回答。我在這個問題中包含了我的重做代碼。 – Tim

0

記住,表視圖內的每個小區是可重複使用的,這意味着,當你向上或向下滾動,關閉屏幕的細胞可以被排隊,並從表視圖中移除,然後加入進入表視圖的當前視圖端口,這也是爲什麼您應該記住特定行的狀態並在-tableView:cellForRowAtIndexPath:中配置您的單元格的原因。因此,針對您的問題的解決方案是,在更改segmentedControl的選定索引時,您應該記住它,然後將其分配到-tableView:cellForRowAtIndexPath:

相關問題