2010-04-16 85 views
0

这是我的计划如何在不使用nib文件的情况下设置单元的高度?

- (void)viewDidLoad { 
    [super viewDidLoad]; 

// Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    self.title = @"Library"; 
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleBordered target:self action:@selector(close:)]; 
    // self.tableView.rowHeight = 80; 
    } 

-(void)close:(id)sender 
{ 
// 
} 


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

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 

    UILabel *dateLabel = [[UILabel alloc]init]; 
    dateLabel.frame = CGRectMake(85.0f, 6.0f, 200.0f, 20.0f); 
    dateLabel.tag = tag1; 
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    cell.contentView.frame = CGRectMake(0.0f, 0.0f, 320.0f, 80.0f); 
    [cell.contentView addSubview:dateLabel]; 
    [dateLabel release]; 
} 

// Set up the cell... 
//[(UILabel *)[cell viewWithTag:tag1] setText:@"Date"]; 
cell.textLabel.text = @"Date"; 

return cell; 
} 

我设置单元的帧速大小的tableView:但细胞是唯一的默认大小。我的意思是我设定的高度是80,但没有设定为80高度。 我该如何做到。

谢谢

回答

1

使用self.tableView.rowHeight = 80.0;,或者如果这不起作用(取决于您的表视图设置),如果您希望不同的单元格具有不同的行高,请使用-[UITableViewDelegate tableView:heightForRowAtIndexPath:]方法。

例如把它放在你的UITableViewDelegate(可能与上面相同的文件)中。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (someConditionBasedOnIndexPath) return 60.0; 
    return 80.0; 
} 
+0

谢谢。 它的工作。我用 - [UITableViewDelegate tableView:heightForRowAtIndexPath:] – 2010-04-16 09:38:45

1

tableView:cellForRowAtIndexPath:如果tableView:numberOfRowsInSection:返回正值,才会调用。你的代码看起来很好,但你不需要设置contentView的框架。

+0

我需要在每个单元格中添加图像和6个标签。所以,我正在使用设置的框架。谢谢。 – 2010-04-16 06:43:49

+0

我认为单元格和contentView框架都是根据rowHeight和表格宽度自动调整大小的。 – drawnonward 2010-04-16 17:13:23

相关问题