UITableView with columns
I need to display a list items in a tableview but with about 5 different columns similar to a spreadsheet. The last column 开发者_如何学Gowould be a UIButton (which I know is possible).
Is it possible to achieve this type of design with the UITableView?
I found an easier way of doing this by following one of Apple's samples:
- (UITableViewCell *)tableViewCellWithReuseIdentifier:(NSString *)identifier {
/*
Create an instance of UITableViewCell and add tagged subviews for the name, local time, and quarter image of the time zone.
*/
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
/*
Create labels for the text fields; set the highlight color so that when the cell is selected it changes appropriately.
*/
UILabel *label;
CGRect rect;
// Create a label for the time zone name.
rect = CGRectMake(LEFT_COLUMN_OFFSET, (ROW_HEIGHT - LABEL_HEIGHT) / 2.0, LEFT_COLUMN_WIDTH, LABEL_HEIGHT);
label = [[UILabel alloc] initWithFrame:rect];
label.tag = NAME_TAG;
label.font = [UIFont boldSystemFontOfSize:MAIN_FONT_SIZE];
label.adjustsFontSizeToFitWidth = YES;
[cell.contentView addSubview:label];
label.highlightedTextColor = [UIColor whiteColor];
[label release];
// Create a label for the time.
rect = CGRectMake(MIDDLE_COLUMN_OFFSET, (ROW_HEIGHT - LABEL_HEIGHT) / 2.0, MIDDLE_COLUMN_WIDTH, LABEL_HEIGHT);
label = [[UILabel alloc] initWithFrame:rect];
label.tag = TIME_TAG;
label.font = [UIFont systemFontOfSize:MAIN_FONT_SIZE];
label.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview:label];
label.highlightedTextColor = [UIColor whiteColor];
[label release];
// Create an image view for the quarter image.
rect = CGRectMake(RIGHT_COLUMN_OFFSET, (ROW_HEIGHT - IMAGE_SIDE) / 2.0, IMAGE_SIDE, IMAGE_SIDE);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect];
imageView.tag = IMAGE_TAG;
[cell.contentView addSubview:imageView];
[imageView release];
return cell;
}
A customized workaround, you can make your own table cells this way:
@interface MyOwnCell : UITableViewCell {
NSMutableArray *columns;
}
- (void)addColumn:(CGFloat)position;
@end
@implementation MyOwnCell
- (void)addColumn:(CGFloat)position {
[columns addObject:[NSNumber numberWithFloat:position]];
}
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0);
CGContextSetLineWidth(ctx, 0.25);
for (int i = 0; i < [columns count]; i++) {
CGFloat f = [((NSNumber*) [columns objectAtIndex:i]) floatValue];
CGContextMoveToPoint(ctx, f, 0);
}
[super drawRect:rect];
}
@end
Refer to TheLearner's solution which is actually more easier :)
精彩评论