我正在从解析中查询图像以显示在我的 tableViewCell 中,其中有 3 个 imageView。因此,如果我有 15 张图片,我将获得 5 行 TableView,并且所有图片都将正确显示。
但是如果我有 16 或 17 张图像,它们不是 3 的倍数,则不会显示图像,这是很合乎逻辑的,因为我已经添加了我的查询计数除以 3。但是我已经被另一个人推荐了一种方法@Wottle 上的非常有用的用户应该可以解决此问题,但它似乎无法正常工作!下面是我的代码,希望有人能帮助我!
非常感谢您的宝贵时间。
即使图像不是 3 的倍数,这个函数也能让我看到图像。
func roundUp(value: Double) -> Int {
if value == Double(Int(value)) {
return Int(value)
} else if value < 0 {
return Int(value)
} else {
return Int(value) + 1
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return roundUp( Double(imageArray.count / 3) )
}
这是我的 cellForRowAtIndexPath:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var counter = (indexPath.row * 3)
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! ProfileTableViewCell
for imageView in cell.threeImages {
imageView.image = UIImage(named: "ImagePlaceHolder")
if counter < imageArray.count {
let finalImage = imageArray[counter++]["image"]
finalImage!.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
imageView.image = UIImage(data:imageData)
}}}}
最好的问候。
最佳答案
imageArray.count
和 3
都是整数,所以将它们相除将得到一个整数。在计算完成后尝试将该整数转换为 double 为时已晚,您必须将它们转换为 double 并对 double 进行除法。
将其更改为 return roundUp(Double(imageArray.count)/3.0)
关于ios - numberOfRowsInSection/3 - 只返回 15 张图像而不是 16 或 17,因为它们不能被 3 整除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35733756/