簡體   English   中英

前5個單元格未在自定義TableView單元格中顯示圖像-iOS

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

我有一個UITableView ,我已經轉化為橫向的tableview,和一個自定義UITableViewCell它只是有一個UIImageViewUILabel

問題是,前5個單元格不顯示圖像,但是當我滾動並返回到它們時,將顯示圖像。 不知道是什么問題:(

(下圖,請參見水平表視圖,忽略垂直視圖) 在此處輸入圖片說明

這是我在TableViewController類中的代碼:

-(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;
}

現在,這是我的自定義單元格類代碼,在其中設置圖像和標簽( _user是屬性,因此將從cell.user自動調用此setter方法):

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

    _user = user;

    _nameLabel.text = @"Hello";

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

為什么不使用Collection View控制器呢? 轉換Tableview控制器似乎不好。

在您的代碼中,您沒有調用setUser方法。

對於自定義表格視圖單元格,您可以在以下位置添加以下代碼

 -(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之前,確保將其清空,然后再分配它,它應該可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM