开发者

How to draw dynamic number of columns in uitableviewcell for iphone in objective c

Can any anyone tell me how to create dynamic number of columns in uitableviewcell using custom class for uitableviewcell. i have 4 or 5 or 6 columns and it is not fixed.so how to draw the columns.

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

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

    CustomCells *cell = (CustomCells*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {

        cell = [[[CustomCells alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    }

    else{
        [[[cell contentView] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];

    }
    for (int i = 0; i < noOfColumns; i++) {

        label= [[UITextView alloc] initWithFrame:CGRectMake(20+(i*columnWidth) , 0 ,(highestWidth + 20), tableView.rowHeight)]; 

        label.frame =CGRectMake(20+(i*columnWidth) , 0 ,(highestWidth + 20), tableView.rowHeight);
        label.text = [[searchedWords objectAtIndex:indexPath.row + (i*dividedWords)] texts];

        label.font = [UIFont fontWithName:@"Papyrus" size:(20.0)];
        label.textColor =[UIColor blackColor];
        label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight; 
     开发者_如何学编程   label.editable = NO;

        label.opaque =YES;

        [cell.contentView addSubview:label];

    }

}

return cell;
}


@darshan is right. In iPhone, there is no concept of columns. You need to use some simple graphics methods to draw rectangles/lines. And then show your label/textview in that rect. So, should look like table of columns & rows.

I had implemented the same, but cant give you the complete code. Just create custom UITableViewCell (subclass it). And write following methods in it

 - (void)addColumn:(CGFloat)position 
 {
if(columns == nil)
{
    columns = [[NSMutableArray alloc]init];
}
     [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);
  CGContextSetRGBStrokeColor(ctx, 0, 0, 0, 1.0);
  CGContextSetLineWidth(ctx, 0.50);

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


Actually in UITableView of iPhone there is no concept of columns. You can create your own custom UITableViewCell and can add subviews to it to make your desired cell. So what can you do is that you create your custom table view cell, add the desired subviews to it and then at run time you decide which views to show or hide. You can do that in cellForRowAtIndexPath method.


I would create multiple cells - a four column cell, a three column cell etc. and choose the right one in cellForRowAtIndexPath.


You can not create columns in UITableView. for more detail : http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITableView_Class/Reference/Reference.html


I found a very nice way of dynamic table view implementation in the below link. With this you can dynamically control the items that need to be added to cell.

iPad-Dynamic-Table-Cells

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜