4

UITableViewCell のサブビュー ( viewz ) を選択したときに背景の変更を除外したい。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;

UIView *viewz = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
viewz.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:viewz];

セルが選択されていません。すべて大丈夫です。

http://img32.imageshack.us/img32/1599/screenshot20121129at123.png

セルの色が青に変わりました。大丈夫です。しかし、ビューの背景色を青に変更したくありません。これどうやってするの?

ここに画像の説明を入力

4

3 に答える 3

6

setSelected:animated: メソッドの空の実装を UITableViewCell サブクラスに追加します

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

}
于 2012-11-29T09:48:47.830 に答える
2

背景色に触れる必要があるようです。そうしないと、デフォルトのセルの実装によって変更されます。viewz色付きの境界線を追加すると、背景色を変更する行にコメントを付けても、まだそこにあることがわかります。

#define VIEWZ_TAG 1234

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ... 
    UIView *viewz = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    viewz.backgroundColor = [UIColor redColor];
    viewz.tag = VIEWZ_TAG;
    viewz.layer.borderWidth = 1;
    viewz.layer.borderColor = [UIColor whiteColor].CGColor;
    [cell.contentView addSubview:viewz];
    ...
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([cell isSelected]) {
        UIView *viewz = [cell viewWithTag:VIEWZ_TAG];
        viewz.backgroundColor = [UIColor greenColor];
    }
}

セルが選択されているときにセルを細かく制御したい場合は、カスタムを使用できますUITableViewCell

于 2012-11-29T13:13:38.047 に答える
0

カスタムセルのみを追加し、選択した行で、そのビューの背景色を変更します。

于 2012-11-29T09:58:45.583 に答える