2017-05-02 10 views
0

UIViewクラスでUITableViewを作成しました。スクロールのUITableViewが間違っています

選択行の設定に基づいて、UIImageViewのアルファ値を1.0f に設定すると、行設定のアルファ値を0.2fに選択解除します。これはうまくいきます。

しかし、選択した値(つまりアルファ1.0f)をスクロールすると、選択されていない間違ったセルが強調表示されます。

私が実装した以下のコードを見つけてください。 ご意見をいただければ幸いです。それは、セルを再利用して、なぜこれがあるので、あなたの

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

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

:また、このコード行を使用する必要があります

//コード

- (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

試しinitWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }のloadMoreTableViewCellForTableView –

答えて

0

以前の設定はリセットされません。

0

私が正しく理解していれば、TableViewをスクロールするとすぐにcell.filterSelectionColor.alphaが正しく表示されません。

セルの選択されたプロパティに依存していますが、セルの位置がスクロール時に作成されたときと同じではない場合があります。選択したセルを別の場所(配列など)に保存し、cellForRowAtIndexPathでCellのステータスを更新する必要があります。次のようなものがあります。

- (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

tableViewをリロードすると、同じセルインスタンスが使用され、データが変更されます。あなたがリロード/スクロールすると、cellForRowAtIndexpathメソッドが呼び出され、そこにはどのセルがどのアルファを持つべきかを指定する必要があります。あなたのコードの変更が必要とされる下記

- (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

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

は、選択と選択解除に値を設定し...

NSMutableArray *selectedCells; 

は、変数を初期化して、コントローラにint型のプロパティを作成します。..

- (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]; 
} 
} 

細胞はのtableViewによって再利用されるように..セットアップにcellForRow法からの細胞を必要と..場合(セル== NIL){ 細胞= [[ALLOCのUITableViewCell]この行を追加

- (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; 
} 
関連する問題