次のように、複数のセクションを持つテーブル ビューがあります。
セル/行を選択するための背景色を設定する方法を知っています
しかし、個々のセル選択ではなく、セクション全体に選択を埋める必要があります
はいの場合、どのように解決策がありますか
次のように、複数のセクションを持つテーブル ビューがあります。
セル/行を選択するための背景色を設定する方法を知っています
しかし、個々のセル選択ではなく、セクション全体に選択を埋める必要があります
はいの場合、どのように解決策がありますか
私はそれがうまくいくことを願っています....
- (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];
}
}
didSelectRowAtIndexPath
tableviewのデリゲートからsectionの値を渡し、 cellForRowAtIndexPath
dataSourceのメソッドでセルの背景色を変更する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];
}
.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;
}
}