2012-04-12 40 views
2

我知道如何創建一個單列和多行的tableview,但我不知道如何創建一個具有多行和多列的tableview。如何創建具有多行和多列的tableview?

任何人都可以幫助我嗎?

+0

你應該創建自定義tableviewcell,如果你需要它看起來像多個列 – Buron 2012-04-12 07:08:25

+0

@Buron你會請教關於我的問題的任何教程嗎? – kumar 2012-04-12 07:11:22

回答

0
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"MyCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
    // load cell from nib to controller's IBOutlet 
    [[NSBundle mainBundle] loadNibNamed:@"MyTableCellView" owner:self options:nil]; 
    // assign IBOutlet to cell 
    cell = myCell; 
    self.myCell = nil; 
    } 

    id modelObject = [myModel objectAtIndex:[indexPath.row]]; 

    UILabel *label; 
    label = (UILabel *)[cell viewWithTag:1]; 
    label.text = [modelObject firstField]; 

    label = (UILabel *)[cell viewWithTag:2]; 
    label.text = [modelObject secondField]; 

    label = (UILabel *)[cell viewWithTag:3]; 
    label.text = [modelObject thirdField]; 

    return cell; 
} 

我認爲這個代碼將會幫助你UITableView並不是真正爲m設計的多個列。但是你可以通過創建一個自定義的UITableCell類來模擬列。在Interface Builder中構建自定義單元格,爲每列添加元素。給每個元素一個標籤,以便您可以在控制器中引用它。

給您的控制器的插座從筆尖加載細胞:

@property(nonatomic,retain)IBOutlet UITableViewCell *myCell; 

然後,在你表視圖委託的的cellForRowAtIndexPath方法中,通過標籤分配這些值。

2

這是我做的:

#import <Foundation/Foundation.h> 

    @interface MyTableCell : UITableViewCell 

{ 
NSMutableArray *columns; 
} 

- (void)addColumn:(CGFloat)position; 

@end 

實現:

#import "MyTableCell.h" 

#define LINE_WIDTH 0.25 

@implementation MyTableCell 

- (id)init 
{ 
self = [super init]; 
if (self) { 
    // Initialization code here. 
} 

return self; 
} 

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

- (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, LINE_WIDTH); 

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]; 
} 

@end 

和最後一塊,的cellForRowAtIndexPath

MyTableCell *cell = (MyTableCell *)[rankingTableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
cell    = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
相關問題