2010-04-16 81 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

相關問題