开发者

How to set the the height of cell programmatically without using nib file?

This is my program

  - (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 *)tableV开发者_如何学Ciew:(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;
}  

I am setting the frame size of cell in tableView: but the cell is in default size only. I mean the height I set was 80 but it was not set as 80 height. How can I make it.


Use self.tableView.rowHeight = 80.0;, or if that's not working (depends on your table view setup), use the -[UITableViewDelegate tableView:heightForRowAtIndexPath:] method if you want different row heights for different cells.

e.g. put this in whatever object is your UITableViewDelegate (probably the same file as above).

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


tableView:cellForRowAtIndexPath: is only called if tableView:numberOfRowsInSection: returns a positive value. Your code looks fine, although you should not need to set the frame of the contentView.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜