【翻译】在iPhone上使用UITableView开发九宫格视图

2009年12月28日 | laifangwen

原文标题

Drawing a Grid in a UITableView

by tjs on October 31, 2008

 

UITableView is probably the most used view on the iPhone. It’s flexible and the UI is ideally suited to use on the iPhone. There are lots of examples on how to add multiple items to a UITableViewCell. However, I needed to present some data in a more traditional spreadsheet style grid. The results worked well and enabled me to pack a lot of information on the screen that was very hard to follow without the vertical grid. I’ll show a very simplified version here you can use to add vertical lines to your UITableView.

 

First we need to create a subclass of UITableViewCell. This is so we can override drawrect and draw our lines and to add an array to hold a list of positions where we’ll draw the lines.

@interface MyTableCell : UITableViewCell {
NSMutableArray *columns;
}
- (void)addColumn:(CGFloat)position;
@end

In this simplified example we’ll leave the positioning of the actual text in the cells in the UITableViewController and place it manually (full source code is attached at the end). We’re just providing a mechanism for drawing vertical lines to make a grid. Column locations are added by calling addColumn:

- (void)addColumn:(CGFloat)position {
[columns addObject:[NSNumber numberWithFloat:position]];
}

Now lets override drawRect. In it we grab the current graphics context and set the line color and width. Then we iterate over our columns array drawing a line from the top of the cell row to the bottom at each position stored in the array.

- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
// Use the same color and width as the default cell separator for now
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);
CGContextAddLineToPoint(ctx, f, self.bounds.size.height);
}

CGContextStrokePath(ctx);

[super drawRect:rect];
}

 

To add columns to the view just call

[cell addColumn:50];

when you’re building each cell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];

MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil) {
cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];

UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 30.0,
tableView.rowHeight)] autorelease];
[cell addColumn:50];
label.tag = LABEL_TAG;
label.font = [UIFont systemFontOfSize:12.0];
label.text = [NSString stringWithFormat:@"%d", indexPath.row];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];

label = [[[UILabel alloc] initWithFrame:CGRectMake(60.0, 0, 30.0,
tableView.rowHeight)] autorelease];
[cell addColumn:120];
label.tag = VALUE_TAG;
label.font = [UIFont systemFontOfSize:12.0];
// add some silly value
label.text = [NSString stringWithFormat:@"%d", indexPath.row * 4];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
}

return cell;
}

That’s it. Being a bit dense I beat my head on my desk a few days before it become obvious how blindingly simple it really was. A lot was just learning ObjectiveC and how UIKit works in general. I’m now working on a GridTableView library that will add a good bit of functionality and ease of use. I’ll post it here.

Here’s the source code.

文章来源:http://dewpoint.snagdata.com/2008/10/31/drawing-a-grid-in-a-uitableview/

Tags: UITableView  iPhone  实例  object-c  程序  开发  
相关文章:

【分享】iPhone开发之数据保存  (2009-12-26 10:28:23)

【分享】 iPhone开发经典语录集锦  (2009-12-26 10:22:23)

黑莓手机开发入门:如何为BlackBerry黑莓手机开发应用软件  (2009-11-20 11:51:31)

收藏:iPhone开发技术多线程之NSInvocationOperation  (2009-8-28 16:53:10)

Three20 TTPhotoViewController使用向导  (2009-8-28 16:46:54)

UITable View实例教程:分组显示列表  (2009-6-18 11:31:30)

UITableView实例教程:创建Table View的detail view  (2009-6-16 17:32:57)

UITableView使用实例:创建一个简单的Table View  (2009-6-16 11:40:59)

acer aspire 4520g初步成功安装mac os pc版  (2009-3-10 13:56:51)

正则表达式30分钟入门教程  (2007-12-27 12:0:11)

引用通告地址: http://blog.laifangwen.com/cmd.asp?act=gettburl&id=257

评论: 0 | 引用: 0 | 浏览:
名称(*):
邮箱:
网站链接:
正文(*):
选 项:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。