2017-05-02 5 views
0

Ich habe UITableView auf UIView-Klasse erstellt.UITableView beim Blättern geht schief

Basierend auf der Auswahl Zeile Einstellung der Alpha-Wert von UIImageView zu 1.0f auf die Zeile Einstellung Alpha-Wert auf 0,2 f, die gut funktionieren.

Aber beim Scrollen wird der ausgewählte Wert (d. H. Alpha 1.0f) mit falscher Zelle hervorgehoben, die überhaupt nicht ausgewählt wurde.

Finden Sie den folgenden Code, den ich implementiert habe. Ihr Feedback wird uns sehr freuen.

//-Code

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [colorNameList count]; // count is century. 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    return [self loadMoreTableViewCellForTableView:tableView indexPath:indexPath]; 

} 

- (FilterColorTableViewCell *)loadMoreTableViewCellForTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath { 

    FilterColorTableViewCell *cell = (FilterColorTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FilterColorTableViewCell"]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    [cell.filterColorImageView setBackgroundColor:[UIColor colorWithHexString:[[[ColorModelClass colorListNames]allValues] objectAtIndex:indexPath.row]alpha:1]]; 
    cell.lbl_FilterColorName.text = [colorNameList objectAtIndex:indexPath.row]; 
    cell.lbl_FilterColorCount.text = [NSString stringWithFormat:@"Items: %ld",(long)indexPath.row]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 

    FilterColorTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    self.filterColorTableView.allowsMultipleSelection = YES; 
    cell.filterSelectionColor.alpha = (cell.selected ?1.0f:0.2f); 

} 

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    FilterColorTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.filterSelectionColor.alpha = (cell.selected ?1.0f:0.2f); 
} 

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return UITableViewAutomaticDimension; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    return UITableViewAutomaticDimension; 
} 
+0

Versuchen Sie diese Zeile in if (Zelle == null) {Zugabe Zelle = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier]; } in loadMoreTableViewCellForTableView –

Antwort

0

Sie auch diese Codezeile verwenden sollte:

cell.filterSelectionColor.alpha = (cell.selected ?1.0f:0.2f); 

in Ihrem

- (FilterColorTableViewCell *)loadMoreTableViewCellForTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {

, weil es die Zelle wieder verwendet und deshalb Ihre vorherigen Einstellungen werden nicht zurückgesetzt.

0

Wenn ich Sie richtig verstanden habe, ist cell.filterSelectionColor.alpha nicht korrekt, sobald Sie durch die TableView blättern, nicht wahr?

Sie verlassen sich auf die Eigenschaft Cell selected, obwohl die Zellenposition möglicherweise nicht mit der Position identisch ist, zu der sie beim Scrollen erstellt wurden. Sie sollten die ausgewählten Zellen an einem anderen Ort (etwa einem Array) speichern und den Zellenstatus in cellForRowAtIndexPath aktualisieren. Etwas wie:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    return [self loadMoreTableViewCellForTableView:tableView indexPath:indexPath]; 

} 

- (FilterColorTableViewCell *)loadMoreTableViewCellForTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath { 

    FilterColorTableViewCell *cell = (FilterColorTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FilterColorTableViewCell"]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    [cell.filterColorImageView setBackgroundColor:[UIColor colorWithHexString:[[[ColorModelClass colorListNames]allValues] objectAtIndex:indexPath.row]alpha:1]]; 
    cell.lbl_FilterColorName.text = [colorNameList objectAtIndex:indexPath.row]; 
    cell.lbl_FilterColorCount.text = [NSString stringWithFormat:@"Items: %ld",(long)indexPath.row]; 

    // selectedItems is an array of booleans with as many elements as the TableView 
    cell.filterSelectionColor.alpha = self.selectedItems[indexPath.row] ? 1.0f : 0.2f; 

    return cell; 
} 
0

Wenn tableView neu lädt, verwendet es die gleiche Zelleninstanz und ändert die Daten. Wenn Sie neu laden/blättern, wird Ihre cellForRowAtIndexpath Methode aufgerufen und dort müssen Sie angeben, welche Zelle welches Alpha haben soll. Unten in Ihrem Code geändert werden muss:

- (FilterColorTableViewCell *)loadMoreTableViewCellForTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath { 

    FilterColorTableViewCell *cell = (FilterColorTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FilterColorTableViewCell"]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    [cell.filterColorImageView setBackgroundColor:[UIColor colorWithHexString:[[[ColorModelClass colorListNames]allValues] objectAtIndex:indexPath.row]alpha:1]]; 
    cell.lbl_FilterColorName.text = [colorNameList objectAtIndex:indexPath.row]; 
    cell.lbl_FilterColorCount.text = [NSString stringWithFormat:@"Items: %ld",(long)indexPath.row]; 

    // Set alpha here 
    cell.filterSelectionColor.alpha = (cell.selected ?1.0f:0.2f); 
    return cell; 
} 
0

eine int-Eigenschaft in Ihrem Controller

NSMutableArray *selectedCells; 

die Variable initialisieren ..

- (void)viewDidload { 
... 
... 
selectedCells = [NSMutableArray array]; 
} 

setzen Sie den Wert auf Auswahl und Abwahl erstellen ..

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
... 
... 
[selectedCells addObject:[NSNumber numberWithInt:indexPath.row]]; 
} 

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ 
... 
... 
for (int i=0;i<selectedCells.count;i++) { 
    if([selectedCells[i] intValue] == indexPath.row) 
     [selectedCells removeObjectAtIndex:i]; 
} 
} 

Da die Zellen durch die Tableview wiederverwendet werden .. Sie benötigen die Zelle aus der cellForRow Methode zur Einrichtung ..

- (FilterColorTableViewCell *)loadMoreTableViewCellForTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath { 

    FilterColorTableViewCell *cell = (FilterColorTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FilterColorTableViewCell"]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

... 
... 
BOOL selected = [selectedCells containsObject:[NSNumber numberWithInt:indexPath.row]]; 
cell.filterSelectionColor.alpha = (selected) ?1.0f:0.2f; 

    return cell; 
} 
Verwandte Themen