1

次のように、複数のセクションを持つテーブル ビューがあります。

ここに画像の説明を入力

セル/行を選択するための背景色を設定する方法を知っています

しかし、個々のセル選択ではなく、セクション全体に選択を埋める必要があります

はいの場合、どのように解決策がありますか

4

3 に答える 3

2

私はそれがうまくいくことを願っています....

 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow];
     if (currentSelectedIndexPath != nil)
     {
        [[tableView cellForRowAtIndexPath:currentSelectedIndexPath] setBackgroundColor:NotSelectedCellBGColor];
     }

     return indexPath;
 }

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:SelectedCellBGColor];
  }

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
 {
   if (cell.isSelected == YES)
   {
       [cell setBackgroundColor:SelectedCellBGColor];
   }
   else
   {
       [cell setBackgroundColor:setBackgroundColor:NotSelectedCellBGColor];
   }
 }
于 2013-01-10T12:06:13.260 に答える
1

didSelectRowAtIndexPathtableviewのデリゲートからsectionの値を渡し、 cellForRowAtIndexPathdataSourceのメソッドでセルの背景色を変更するUITableview

ヘッダファイル内

NSUInteger index;

実装ファイル (.m)

-(void) viewDidLoad{
index=-1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CELL_IDENTIFIER";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];


    }

        if (indexPath.section==index) {
            cell.contentView.backgroundColor=[UIColor yellowColor];
        }
        else{
            cell.contentView.backgroundColor=[UIColor clearColor];
        }
       cell.textLabel.text=[NSString stringWithFormat:@"Row %d Section %d",indexPath.row,indexPath.section];
       return cell;
}

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

    index=indexPath.section;
    [tableView reloadData];

}
于 2013-01-10T12:39:42.227 に答える
0

.h

int currentSection;

.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    currentSection = indexPath.section;
    [tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( currentSection == indexPath.section ) {
        cell.backgroundColor = SelectedColor;
    } else {
        cell.backgroundColor = OtherColor;
    }
}
于 2013-01-10T12:37:14.153 に答える