任何人都可以告诉我如何使用uitableviewcell的自定义类在uitableviewcell中创建动态数量的列。我有4或5或6列,它没有固定。所以如何绘制列。
- (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;
}
答案 0 :(得分:1)
@darshan是对的。在iPhone中,没有列的概念。您需要使用一些简单的图形方法来绘制矩形/线条。然后在那个rect中显示你的标签/ textview。 所以,应该看起来像列和列表行。
我已经实现了相同的功能,但无法提供完整的代码。 只需创建自定义UITableViewCell(它的子类)。并在其中编写以下方法
- (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];
}
答案 1 :(得分:0)
实际上在iPhone的UITableView中没有列的概念。您可以创建自己的自定义UITableViewCell,并可以向其添加子视图以生成所需的单元格。那么您可以做的是创建自定义表格视图单元格,向其添加所需的子视图,然后在运行时决定要显示或隐藏哪些视图。你可以在cellForRowAtIndexPath方法中做到这一点。
答案 2 :(得分:0)
我会创建多个单元格 - 一个四列单元格,一个三列单元格等,并在cellForRowAtIndexPath中选择正确的单元格。
答案 3 :(得分:0)
您无法在UITableView中创建列。 更多详情:http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITableView_Class/Reference/Reference.html
答案 4 :(得分:0)
我在下面的链接中找到了一种非常好的动态表视图实现方式。通过此操作,您可以动态控制需要添加到单元格的项目。