2015-09-05 55 views
1

我面临一个问题与我的一个tableView,当我解码JSON并把我的imageview的大小约束它的作品,但当我滚动tableview或点击图像,imageview将返回到其原始大小。UITableView的UIImageview:在滚动或水龙头意外调整大小

我用一个自定义单元格,以避免这类问题,但它不工作:

第一负载:

First load :

自来水或滚动:

On tap or Scroll

我的自定义单元格:
class CustomCell: UITableViewCell { 


@IBOutlet var picture: UIImageView! 
@IBOutlet var title: UILabel! 
@IBOutlet var price: UILabel! 


override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 

} 

我的tableView FUNC:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    var cell:CustomCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell 

    tableView.tableFooterView = UIView(frame:CGRectZero) 
    tableView.separatorColor = UIColor.whiteColor() 
    tableView.backgroundColor = UIColor(rgb: 0x007AFF) 
    cell.backgroundColor = UIColor(rgb: 0x007AFF) 

    cell.title.text = repos[indexPath.row].title 
    cell.price.text = repos[indexPath.row].price 

    let urlString = repos[indexPath.row].picture_url! as String, 
    imgURL = NSURL(string: urlString as String) 

     // If this image is already cached, don't re-download 
     if let img = imageCache[repos[indexPath.row].picture_url!] { 
      cell.picture.image = img 
      cell.picture.frame = CGRectMake(5,0,61,43) 
      println("image présente") 
     } 

     else { 

      loading.startAnimating() 
      // The image isn't cached, download the img data 
      // We should perform this in a background thread 
      let request: NSURLRequest = NSURLRequest(URL: imgURL!) 
      let mainQueue = NSOperationQueue.mainQueue() 
      NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue, completionHandler: { (response, data, error) -> Void in 
       if error == nil { 
        // Convert the downloaded data in to a UIImage object 
        let image = UIImage(data: data) 
        // Store the image in to our cache 
        self.imageCache[repos[indexPath.row].picture_url!] = image 
        // Update the cell 

        dispatch_async(dispatch_get_main_queue(), { 
         if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) { 
          cellToUpdate.imageView?.image = image 
          cellToUpdate.imageView?.frame = CGRectMake(5,0,61,43) 

          println("image reload") 
         } 
        }) 
       } 
       else { 
        println("Error: \(error.localizedDescription)") 
       } 
      }) 
     loading.stopAnimating() 
     } 
    return cell 
} 

你有没有从问题就来了,其中一个想法?

谢谢

+0

得到一个答案? –

+0

是的,看看我下面的评论。 – f1rstsurf

回答

0

最简单的方法来解决问题是您在CustomCell设置自动版式。

您只需设置宽度和高度即可。与此类似,

enter image description here

作为补充,最好是根据需要来调整的UIImageView的contentMode和clipsToBounds。

+0

好吧,我试过了,现在它可以工作,但只适用于第一排!其他人仍在调整大小 – f1rstsurf

0

好吧,我解决了问题,实际上在我的代码中,如果我的图片还没有被缓存,我下载它,当它完成后,我把一个新的tableviewcell(cellToUpdate),我没有UIImageView定义对于这个单元格,顺便说一句,我使用了默认的“imageView?”,它将图像调整为默认值。

我刚刚更换我的cellToUpdate由我customCell的名字,我的自定义ImageView的,和它的作品:

cell.picture.image = image 
cell.picture.frame = CGRectMake(5,0,61,43)