Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a tableview with multiple rows and multiple columns?

I know how to create a tableview with a single column and multiple rows, but I don't know how to create a tableview with multiple rows and multiple columns.

Can anybody help me?

like image 549
kumar Avatar asked Jan 31 '26 16:01

kumar


1 Answers

This is how i did it:

#import <Foundation/Foundation.h>

  @interface MyTableCell : UITableViewCell 

{   
NSMutableArray *columns;
 }

- (void)addColumn:(CGFloat)position;

@end

implementation:

#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

and the last piece, cellForRowAtIndexPath

MyTableCell *cell = (MyTableCell *)[rankingTableView dequeueReusableCellWithIdentifier:MyIdentifier];
cell              = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
like image 80
Andrey Chernukha Avatar answered Feb 03 '26 04:02

Andrey Chernukha