├── ABTableViewCell.h └── ABTableViewCell.m /ABTableViewCell.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2008 Loren Brichter 2 | // 3 | // Permission is hereby granted, free of charge, to any person 4 | // obtaining a copy of this software and associated documentation 5 | // files (the "Software"), to deal in the Software without 6 | // restriction, including without limitation the rights to use, 7 | // copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | // copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following 10 | // conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be 13 | // included in all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | // OTHER DEALINGS IN THE SOFTWARE. 23 | // 24 | // ABTableViewCell.h 25 | // 26 | // Created by Loren Brichter 27 | // Copyright 2008 Loren Brichter. All rights reserved. 28 | // 29 | 30 | #import 31 | 32 | // to use: subclass ABTableViewCell and implement -drawContentView: 33 | 34 | @interface ABTableViewCell : UITableViewCell { 35 | UIView* contentView; 36 | UIView* selectedContentView; 37 | } 38 | 39 | - (void)drawContentView:(CGRect)rect highlighted:(BOOL)highlighted; // subclasses should implement 40 | 41 | @end 42 | -------------------------------------------------------------------------------- /ABTableViewCell.m: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2008 Loren Brichter 2 | // 3 | // Permission is hereby granted, free of charge, to any person 4 | // obtaining a copy of this software and associated documentation 5 | // files (the "Software"), to deal in the Software without 6 | // restriction, including without limitation the rights to use, 7 | // copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | // copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following 10 | // conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be 13 | // included in all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | // OTHER DEALINGS IN THE SOFTWARE. 23 | // 24 | // ABTableViewCell.m 25 | // 26 | // Created by Loren Brichter 27 | // Copyright 2008 Loren Brichter. All rights reserved. 28 | // 29 | 30 | #import "ABTableViewCell.h" 31 | 32 | @interface ABTableViewCellView : UIView 33 | @end 34 | 35 | @interface ABTableViewSelectedCellView : UIView 36 | @end 37 | 38 | @implementation ABTableViewCellView 39 | 40 | - (id)initWithFrame:(CGRect)frame { 41 | if((self = [super initWithFrame:frame])) { 42 | self.contentMode = UIViewContentModeRedraw; 43 | } 44 | 45 | return self; 46 | } 47 | 48 | - (void)drawRect:(CGRect)rect { 49 | [(ABTableViewCell *)[self superview] drawContentView:rect highlighted:NO]; 50 | } 51 | 52 | @end 53 | 54 | @implementation ABTableViewSelectedCellView 55 | 56 | - (id)initWithFrame:(CGRect)frame { 57 | if((self = [super initWithFrame:frame])) { 58 | self.contentMode = UIViewContentModeRedraw; 59 | } 60 | 61 | return self; 62 | } 63 | 64 | - (void)drawRect:(CGRect)rect { 65 | [(ABTableViewCell *)[self superview] drawContentView:rect highlighted:YES]; 66 | } 67 | 68 | @end 69 | 70 | 71 | @implementation ABTableViewCell 72 | 73 | - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 74 | if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { 75 | contentView = [[ABTableViewCellView alloc] initWithFrame:CGRectZero]; 76 | contentView.opaque = YES; 77 | self.backgroundView = contentView; 78 | [contentView release]; 79 | 80 | selectedContentView = [[ABTableViewSelectedCellView alloc] initWithFrame:CGRectZero]; 81 | selectedContentView.opaque = YES; 82 | self.selectedBackgroundView = selectedContentView; 83 | [selectedContentView release]; 84 | 85 | } 86 | 87 | return self; 88 | } 89 | 90 | - (void)dealloc { 91 | [super dealloc]; 92 | } 93 | 94 | - (void)setSelected:(BOOL)selected { 95 | [selectedContentView setNeedsDisplay]; 96 | 97 | if(!selected && self.selected) { 98 | [contentView setNeedsDisplay]; 99 | } 100 | 101 | [super setSelected:selected]; 102 | } 103 | 104 | - (void)setSelected:(BOOL)selected animated:(BOOL)animated { 105 | [selectedContentView setNeedsDisplay]; 106 | 107 | if(!selected && self.selected) { 108 | [contentView setNeedsDisplay]; 109 | } 110 | 111 | [super setSelected:selected animated:animated]; 112 | } 113 | 114 | - (void)setHighlighted:(BOOL)highlighted { 115 | [selectedContentView setNeedsDisplay]; 116 | 117 | if(!highlighted && self.highlighted) { 118 | [contentView setNeedsDisplay]; 119 | } 120 | 121 | [super setHighlighted:highlighted]; 122 | } 123 | 124 | - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { 125 | [selectedContentView setNeedsDisplay]; 126 | 127 | if(!highlighted && self.highlighted) { 128 | [contentView setNeedsDisplay]; 129 | } 130 | 131 | [super setHighlighted:highlighted animated:animated]; 132 | } 133 | 134 | - (void)setFrame:(CGRect)f { 135 | [super setFrame:f]; 136 | CGRect b = [self bounds]; 137 | // b.size.height -= 1; // leave room for the seperator line 138 | [contentView setFrame:b]; 139 | [selectedContentView setFrame:b]; 140 | } 141 | 142 | - (void)setNeedsDisplay { 143 | [super setNeedsDisplay]; 144 | [contentView setNeedsDisplay]; 145 | 146 | if([self isHighlighted] || [self isSelected]) { 147 | [selectedContentView setNeedsDisplay]; 148 | } 149 | } 150 | 151 | - (void)setNeedsDisplayInRect:(CGRect)rect { 152 | [super setNeedsDisplayInRect:rect]; 153 | [contentView setNeedsDisplayInRect:rect]; 154 | 155 | if([self isHighlighted] || [self isSelected]) { 156 | [selectedContentView setNeedsDisplayInRect:rect]; 157 | } 158 | } 159 | 160 | - (void)layoutSubviews { 161 | [super layoutSubviews]; 162 | self.contentView.hidden = YES; 163 | [self.contentView removeFromSuperview]; 164 | } 165 | 166 | - (void)drawContentView:(CGRect)rect highlighted:(BOOL)highlighted { 167 | // subclasses should implement this 168 | } 169 | 170 | @end 171 | --------------------------------------------------------------------------------