你的代码看起来模糊,虽然我您在使用自定义单元格级的同意。这样做是为了尝试下载所有图像集,如果无法下载,它将用保存在资产上的图像占位符替换图像。所有图像在加载时都具有相同的尺寸。
以下是您可以做的工作。这是您的自定义课程。
class SomeCell: UITableViewCell {
@IBOutlet weak var imgMain: UIImageView!
@IBOutlet weak var lblMain: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
imgMain.layer.cornerRadius = 5.0
imgMain.clipsToBounds = true
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func configureCell(image: UIImage, text: String){
imgMain.image = image
lblMain.text = text
}
}
然后在您的视图控制器
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var somePics = ["http://www.something.com/pic1.jpg","http://www.something.net/pic2.jpg","http://www.something.com/pic3.jpg","http://www.something.com/pic4.jpg","http://www.something.net/pic5.jpg"]
var someTitles = ["Picture 1", "Picture 2", "Picture 3", "Picture 4", "Picture 5"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("SomeCell") as? SomeCell {
var img: UIImage!
let url = NSURL(string: somePics[indexPath.row])!
if let data = NSData(contentsOfURL: url) {
img = UIImage(data: data)
}else {
img = UIImage(named: "somePlaceholderpic")
}
cell.configureCell(img, text: someTitles[indexPath.row])
return cell
} else {
return SomeCell()
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return somePics.count
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
希望这有助于。 :)
是的,你需要添加约束细胞的含有观点和没有必要设置ImageView的框架编程。 例如:http://postimg.org/image/utqzbrjn1/ –
你可以在storyboard中给你的tableviewCell截图吗?如果您向我们展示您的约束条件,它会有所帮助广告将很高兴在'cellForRowAtIndexPath'下显示您的完整代码。 –