Overlapping when using custom Cells in UITableView

Asked 2 years ago, Updated 2 years ago, 146 views

Creating TableViews using custom Cells without InterfaceBuilder

In ViewController, add UITableView to
You have created a CustomCell class and added the CustomView class to it.

ContentView.

In ViewController

[_tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"Cell";


Launching Using the Multiple cells will appear in one cell

ViewController.m

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
    CustomCell* cell= [tableView requestReusableCellWithIdentifier:@"Cell";
    return cell;
}

CustomCell.m

 - (id) initWithStyle: (UITableViewCellStyle) style reuseIdentifier: (NSString*) reuseIdentifier
    {
        self=[super initWithStyle: style reuseIdentifier: reuseIdentifier];

        if(self){
            for (UIView* subview in [self.contentView subviews]) {
                [subview removeFromSuperview];
            }

            _dateLabel=[[UILabel alloc] initWithFrame: CGRectMake(16,0,320-(16*2),20)];
            _dateLabel.font = [UIFont systemFontOfSize:15];
            _dateLabel.text = [[NSDate date] description];
            [self.contentView addSubview:self.dateLabel];

            // Views here may appear double when scrolling
            CGRect rect1 = CGRectMake(0,20,160,20);
            _view1 = [[CustomView alloc] initWithFrame: rect1];
            [self.contentView addSubview:_view1];

            CGRect rect2 = CGRectMake (160, 20, 160, 20);
            // AddSubView Additional Views
        }
        return self;
    }

CustomView.m

-(id)initWithFrame:(CGRect)frame
    {
        self=[super initWithFrame:frame];
        if(self){
            // initialization code
            [self initializeView:frame];
        }
        return self;
    }
    - (void) initializeView: (CGRect) frame
    {
        for (UIView* subview in [self subviews]) {
            [subview removeFromSuperview];
        }

        _titleLabel = [[UILabel alloc] init];
        _titleLabel.frame = CGRectMake (44+16,0,160-44,22);
        _titleLabel.text=@"text";
        self addSubview:_titleLabel;

        _conditionLabel = [[UILabel alloc] init];
        _conditionLabel.frame = CGRectMake (44+16,22+4,160-44,22);
        _conditionLabel.text=@"text2";
        self addSubview:_conditionLabel;
    }

I think the reason is that cellForRowAtIndexPath is reusing the cell with the ID @"Cell", but
registerClass has fixed IDs, so you cannot say that you do not want to reuse them with multiple IDs.
Is there a way to use a custom cell to prevent the display from overlapping when the cell is addView?

Displayed like this

ios objective-c uitableview

2022-09-30 20:51

1 Answers

What I can judge from the scope of the screenshot is that the height of the table row does not match the height of each cell.Please note that the table row height is not automatically adjusted to fit the cell size (*).
Adjust the table row height with the UITableView property rowHeight.In order to prevent cells from overlapping, at least 2.7 times the current level is required.
Note that the UITableViewDataSource protocol uses the -(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath method.

AThere seems to be a way to automatically adjust between lines according to the size of the cell using Auto Layout.


2022-09-30 20:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.