如何在目标C中为iPhone绘制UITableViewCell中的动态列数 [英] How to draw dynamic number of columns in uitableviewcell for iphone in objective c
问题描述
任何人都可以告诉我如何使用uitableviewcell的自定义类在uitableviewcell中创建动态列数.我有4或5或6列,并且它不是固定的.所以如何绘制列.
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是正确的.在iPhone中,没有列的概念.您需要使用一些简单的图形方法来绘制矩形/线条.然后在该区域显示您的标签/文本视图. 因此,应看起来像列表行.
@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.
我已经实现了相同的功能,但是无法提供完整的代码. 只需创建自定义UITableViewCell(将其子类化)即可.并在其中编写以下方法
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];
}
这篇关于如何在目标C中为iPhone绘制UITableViewCell中的动态列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!