2015-10-06 53 views
1

我想通過目標c代碼在每個單元格右側的UITableViewCell中顯示一個imageview。我使用了下面的代碼,但它在左側顯示圖像。框架未設置。如何更改tableCell的imageView框架?

UIImageView *bgView = [[UIImageView alloc]initWithFrame:CGRectMake(cell.frame.size.width/2, 0, cell.frame.size.width/2, cell.frame.size.height)]; 

     if([[self.categories objectAtIndex:indexPath.section] isKindOfClass:[SpecialPagesCategory class]]) { 
      cell.backgroundColor=[UIColor whiteColor]; 
      bgView.image = [UIImage imageNamed:@"fbBtn.png"]; 
     } else { 
      cell.backgroundColor=[UIColor clearColor]; 
      bgView.image = nil; 
     } 

     cell.imageView.image = bgView.image; 
     cell.imageView.frame = bgView.frame; 

如何通過我們的自定義框架重置UITableViewCell imageView?和我創建一個細胞就像如下

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell==nil) { 
     cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
     [self.categoryTable flashScrollIndicators]; 
     cell.textLabel.font=[UIFont fontWithName:OPEN_SANS size:16.0]; 
     cell.backgroundColor=[UIColor clearColor]; } 

我已經通過內容的瀏覽也試過如下

UIImageView *bgView = [[UIImageView alloc]initWithFrame:CGRectMake(cell.frame.size.width/2, 0, cell.frame.size.width/2, cell.frame.size.height)]; 
if([[self.categories objectAtIndex:indexPath.section] isKindOfClass:[SpecialPagesCategory class]]) { 
      cell.backgroundColor=[UIColor whiteColor]; 
      bgView.image = [UIImage imageNamed:@"fbBtn.png"]; 
      [cell.contentView addSubview: bgView]; 
     } else { 
      cell.backgroundColor=[UIColor clearColor]; 
      bgView.image = nil; 
      [cell.contentView addSubview: bgView]; 
     } 

問題與「內容視圖」是ImageView的是不是不滿足的單元格中刪除if條件

+2

你爲什麼不使用自定義單元格? – Jan

+0

已經編碼沒有自定義單元格,所以小組負責人告訴沒有自定義單元格 – Sakthimuthiah

+1

爲什麼你使用'cell.imageView'。像創建'bgView'並設置你的框架一樣創建自己的圖像視圖。 –

回答

1

創建圖像視圖並將其作爲子視圖添加到單元格中,它將起作用。

UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 50, 50)];// Your specification 


imgView.backgroundColor=[UIColor clearColor]; 
[imgView.layer setCornerRadius:5.0f]; 
[imgView.layer setMasksToBounds:YES]; 
[imgView setImage:[UIImage imageWithData: imageData]]; 
[ cell.contentView addSubview:imgView]; 
+0

此代碼對於編寫良好的項目並不合適。首先,它不適用於MVC,它是iOS中的基本模式。其次,它不會刷新框架。第三,你沒有任何編碼風格。 – Vive

+0

我已經創建了一個自定義表格視圖單元格並對其進行了修復。謝謝 – Sakthimuthiah

+0

好。這是最好的解決方案。 – Jan

1

重寫此方法:

- (void)viewWillLayoutSubviews { 
    [super viewWillLayoutSubviews]; 

    [self.myImageView setFrame:CGRectMake(0, 0, 200, 200)]; 
} 

OFC在哪裏,你應該用你的圖像視圖和幀的設置適當的尺寸更換myImageView。

順便說一句,代碼質量提示:我也建議閱讀關於MVC並保留UITableViewCell子代實現文件layoutSubviews方法中的代碼。