まず、テーブルがどのように機能するかについて少し説明します。
UITableView
背景があります。これが細胞の後ろに見えるものです。
- すべてのセルには、 、 、 の 3 つの特別なビューが
contentView
ありbackgroundView
ますselectedBackgroundView
。
backgroundView
セルが選択されている場合、セルの内容の下に表示される が代わりselectedBackgroundView
に使用されます。
- 通常、すべてのコンテンツは
contentsView
. コンテンツから背景を分離することで、テーブルは選択されていない/選択されているトランジションを適切にアニメーション化できます。また、セルが編集モードに入るときも重要です。コンテンツは縮小/移動され、編集コントロール (削除ボタンやセル選択など) はコンテンツ上で独立して表示できます。
- セルには、区切りビューも直接含まれています (通常、セルの下部に 1 または 2 ピクセルの高さのビューがあります
separatorStyle
)。そのため、セルは常にそのセルよりも少なくとも 1 ピクセル高くなりますcontentsView
。
次に、グループ化されたテーブルがどのように機能するかについて少し説明します。
- テーブルには特別なデフォルトの背景があります。
backgroundView
と を使用して削除/変更できますbackgroundColor
。
- グループ化されたテーブル ビューのセルは小さくなり
contentView
ます。セルの幅はテーブル全体と同じですが、contentView
左右にオフセットがあります (iPhone では約 10 ポイント)。
backgroundView
が変更され、セルの位置に応じて境界線を描画するレイヤーが含まれます (最初、最後、および中間のセルで異なります) 。ボーダーは、テーブルの で指定された色を持ちますseparatorColor
。
コード内ですべてを変更できます。
境界線を削除する最も簡単な方法の 1 つは、に設定separatorColor
すること[UIColor clearColor]
です。これにより、セルセパレーターが削除されますが、独自のセパレーターを追加できます。
cell = ...
UIView* separator = [[UIView alloc] init];
separator.backgroundColor = ...
separator.frame = CGRectMake(0.0f, table.bounds.size.width, table.rowHeight - 1.0f, 1.0f);
separator.autoresizingMask = (UIViewAutoresizingMaskFlexibleTopMargin | UIViewAutoresizingMaskFlexibleWidth);
[cell addSubview:separator]; //note we are adding it directly to the cell, not to contentsView
UIImageView
単色ビューの代わりに、セパレーターにイメージ ( ) を使用することもできます。
別の方法は、各セルにbackgroundView
toを設定することです。nil
実際には、を完全に無視してcontentsView
、すべてをセルに直接追加できます。その後、次のセル メソッドをカスタマイズして、状態が変化したときにセルを更新できます。
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//update the cell text colors, backgrounds etc for the selected/not selected state
//you don't have to call super
// (the default implementation changes between backgroundView and selectedBackgroundView)
}
- (void)setHighlighted:(BOOL)highlited animated:(BOOL)animated {
//update the cell text colors, backgrounds etc for the selected/not highlighted state
//you don't have to call super
// (the default implementation changes between backgroundView and selectedBackgroundView)
//by giving empty implementation, you will block highlighting of cells
}
- (void)setEditing:(BOOL)highlited animated:(BOOL)animated {
//you can use this method to add custom editing controls
}
- (void)willTransitionToState:(UITableViewCellStateMask)state {
//use this method to change the layout of your contents
//when the cells goes into one of the editing states
}
- (void)layoutSubviews {
//update the frames of cell contents to match the new cell size
//note that the cell doesn't have the correct size until it is added to the table,
//this method is called when the cell already has the final size
//you can also use this method to change the size/position of the `contentsView`
}
編集:
特定の問題に対処するには、おそらく最良の解決策は次のとおりです。
- 表からデフォルトを削除し、
backgroundView
独自の背景を追加します (例: クリア カラー、ホワイト カラー、パターンから作成されたカラー、UIImageView
またはbackgroundView
.
backgroundView
すべてのセルからデフォルトを削除し、 UIImageView
. 最初、最後、および中間のセルには、3 つの特別な画像が必要です。