简体   繁体   中英

How to set the same width between custom UITableViewCells and UITableView?

I created several cells with Interface Builder, and I'm using them to fill a UITableView. In other words, I have 3 classes for 3 different kinds of cell, and an other view which contains a UITableView.

- My UITableView containing different kinds of cells : 在此处输入图片说明

Here's my problem : On the iPhone emulator, it looks great. But on the iPad emulator, the custom cells width is fixed . The UITableView width fits to the screen width, so it's good, but the UITableViewCells does not fit to the UITableView. I want to force the custom UITableViewCells to take the UITableView width.

Is there anything to do in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method, where I instanciate my custom cells ? Or do I have to write a thing like self.fitToParent; in the custom cells header file ?

EDIT (schema) :

在此处输入图片说明

EDIT 2 (cellForRowAtIndexPath method) :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifierType1 = @"cellType1";
    static NSString *cellIdentifierType2 = @"cellType2";
    NSString *currentObjectId = [[myTab objectAtIndex:indexPath.row] type];

    // Cell type 1
    if ([currentObjectId isEqualToString:type1])
    {
        CelluleType1 *celluleType1 = (CelluleType1 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifierType1];

        if(celluleType1 == nil)
            celluleType1 = [[CelluleType1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierType1];

        celluleType1.lblAuteur.text = @"Type1";

        return celluleType1;
    }

    // Cell type 2
    else if ([currentObjectId isEqualToString:type2])
    {
        CelluleType2 *celluleType2 = (CelluleType2 *)[tableViewdequeueReusableCellWithIdentifier:cellIdentifierType2];

        if(celluleType2 == nil)
            celluleType2 = [[CelluleType2 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierType2];

        celluleType2.lblAuteur.text = @"Type2";

        return celluleType2;
    }
    else
        return nil;
}
}

I think uitableviewcell's width is the same as the tableview's width.You can try to set cell's background color to test it. cell.backgroundColor = [UIColor redColor] ;

You should create a class which inherit from UITableViewCell and override it's method - (void)layoutSubviews , adjust your content's frame there.

I resolved my problem using the following code in each custom cell class. It's not very clean, but I can't spend one more day on this issue...

- (void)layoutSubviews
{
    CGRect contentViewFrame = self.contentView.frame;
    contentViewFrame.size.width = myTableView.bounds.size.width;
    self.contentView.frame = contentViewFrame;
}

Thank you for your help KudoCC .

- (void)awakeFromNib {
    [super awakeFromNib];
// anything you write in this section is taken with respect to default frame of width 320.
}

awakeFromNib is called when [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; is processed- anything you write in section is taken with respect to default frame of width 320. You need to make another custom function and call it after cell gets initialized. For eg:-

@implementation CheckinTableViewCell{
    UILabel *NameLabel;
    UILabel *rollLabel;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    NameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    rollLabel = [[UILabel alloc] initWithFrame:CGRectZero];

    [self.contentView addSubview:NameLabel];
    [self.contentView addSubview:rollLabel];

}

-(void) bindView{
    NameLabel.frame = CGRectMake(10, 10, self.contentView.frame.size.width-20, 20);
    rollLabel.frame = CGRectMake(10, 30, NameLabel.frame.size.width, 20);
}

and call this function in tableview cellForRowAtIndex:-

-(UITableViewCell*) tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    CheckinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if(cell ==nil){
        cell = [[CheckinTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

        cell.name = @"Harry";
        cell.rollno = @"123456";

        [cell bindView];

    return cell;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM