我面临一个问题与我的一个tableView,当我解码JSON并把我的imageview的大小约束它的作品,但当我滚动tableview或点击图像,imageview将返回到其原始大小。UITableView的UIImageview:在滚动或水龙头意外调整大小
我用一个自定义单元格,以避免这类问题,但它不工作:
第一负载:
自来水或滚动:
我的自定义单元格: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
}
你有没有从问题就来了,其中一个想法?
谢谢
得到一个答案? –
是的,看看我下面的评论。 – f1rstsurf