简体   繁体   中英

First 5 cells not showing image in custom TableView Cell - iOS

I have a UITableView which I have transformed into horizontal tableview, and a custom UITableViewCell which just has a UIImageView and a UILabel

The problem is, first 5 cells don't show the images, but when I scroll and come back to them, images are shown. No idea what the problem is :(

(picture below, please see the horizontal tableview, ignore the vertical one) 在此处输入图片说明

Here's my code in TableViewController Class:

-(void)viewDidLoad
{
    //Rotating the tableview angle -PI/2 Rad = -90 Degrees
    _friendsTableView.transform= CGAffineTransformMakeRotation(-M_PI_2);

    _friendsTableView.translatesAutoresizingMaskIntoConstraints = YES;
    CGRect frame = _friendsTableView.frame;
    frame.origin.y= _segmentControl.frame.size.height;
    frame.origin.x= 0;
    frame.size.width = self.view.frame.size.width;
    frame.size.height = 105.5;
    _friendsTableView.frame= frame;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    FriendsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    if (nil == cell) {
            cell = [[FriendsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        }

        if(cell)
        {
            cell.user = cellsArray_[indexPath.row];
            cell.transform =CGAffineTransformMakeRotation(M_PI_2);

        }
        return cell;
}

Now this is my custom cell class code where I set the image and label ( _user is a property, so this setter method gets called automatically from cell.user ):

-(void)setUser
{
    _profileImageView.layer.cornerRadius = _profileImageView.frame.size.width/2;

    _user = user;

    _nameLabel.text = @"Hello";

    [_profileImageView setImage:[UIImage imageNamed:@"placeholder_image"]];
}

Why dont you use Collection View controller instead. Transforming Tableview controller seems not good.

In your code your are not calling your setUser method.

For Custom table view cell you can add following code in

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

MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:strID];
            if (cell == nil)
            {
              NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
}

根据我的经验,这与单元的可重用性严格相关,我曾经遇到过类似的问题,我的解决方案是在将某些东西分配给IBOulet之前,确保将其清空,然后再分配它,它应该可以工作。

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