自定义类,从UITableViewCell继承,头文件如下:
#import <UIKit/UIKit.h>
@interface MyTableCell : UITableViewCell {
NSMutableArray *columns;
}
- (void)addColumn:(CGFloat)position;
@end
实现文件如下:
#import "MyTableCell.h"
@implementation MyTableCell
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
// if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) {
// Initialization code
columns = [[NSMutableArray alloc] initWithCapacity:5];
}
return self;
}
- (void)addColumn:(CGFloat)position {
[columns addObject:[NSNumber numberWithFloat:position]];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)drawRect:(CGRect)rect {
if (0 < [columns count]) {
CGContextRef ctx = UIGraphicsGetCurrentContext();
// just match the color and size of the horizontal line
// CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0);
CGContextSetRGBStrokeColor(ctx, 0, 0, 0, 1.0);
// CGContextSetLineWidth(ctx, 0.25);
CGContextSetLineWidth(ctx, 0.5);
for (int i = 0; i < [columns count]; i++) {
// get the position for the vertical line
CGFloat f = [((NSNumber*) [columns objectAtIndex:i]) floatValue];
CGContextMoveToPoint(ctx, f, 0);
CGContextAddLineToPoint(ctx, f, self.bounds.size.height);
}
CGContextStrokePath(ctx);
}
[super drawRect:rect];
}
- (void)dealloc {
[columns release];
[super dealloc];
}
@end
然后在以下函数中,用MyTableCell替代UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{