【Objective-C】UITableViewの編集モードで押下時のチェックマークを違和感なく削除する方法【Xcode10.2対応】(非表示)
こういう人に向けて発信しています。
・UITableViewでチェックマークが不要な人
・チェックマークを削除した後の隙間がいらない人
・Objecvtive-C中級者
前回の記事:編集モードでチェックマークを表示させる
コード(Objective-C)
//インテント調整
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
//チェックマーク分の余白を詰める為にインテントは表示しない。
return NO;
}
/**
@brief セル表示前の処理
*/
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView.editing){
//セル表示直前の処理で非表示状態の時はチェックマーク削除する
for(UIView *tableViewCellEditControl in cell.subviews){
if( [NSStringFromClass(tableViewCellEditControl.class) isEqualToString:@"UITableViewCellEditControl"] ){
tableViewCellEditControl.hidden = YES;
}
}
}
}
上記の実装で可能です。
(1)インデントを詰める
(2)cellのインスタンスを取得して、subViews上で
tableViewCellEditControllerが存在しているか判別してhiddenにする。
補足:編集モード中で押下した時の背景色を変化させたい。
/**
@brief セル表示前の処理
*/
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView.editing){
UIView *selectedCellBackColor = [[UIView alloc]initWithFrame:cell.frame];
selectedCellBackColor.backgroundColor = [UIColor whiteColor];
cell.multipleSelectionBackgroundView = selectedCellBackColor;
}
}
この記事が気に入ったらサポートをしてみませんか?