ios - numberOfRowsInSection/3 - 只返回 15 张图像而不是 16 或 17,因为它们不能被 3 整除

标签 ios swift parse-platform tableview

我正在从解析中查询图像以显示在我的 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.count3 都是整数,所以将它们相除将得到一个整数。在计算完成后尝试将该整数转换为 double 为时已晚,您必须将它们转换为 double 并对 double 进行除法。

将其更改为 return roundUp(Double(imageArray.count)/3.0)

关于ios - numberOfRowsInSection/3 - 只返回 15 张图像而不是 16 或 17,因为它们不能被 3 整除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35733756/

相关文章:

ios - 同时执行cornerRadius和约束动画

ios - 多线程核心数据-NSManagedObject无效

ios - SwiftUI 文本字段 + 步进器

rest - 改造:仅更新特定字段

ios - 自定义键盘的 "Next"和 "Done"返回按钮

swift - 在静态 UITableViewController 之上的 UIView

ios - 在 tvOS 上禁用 AVPlayer 下拉菜单

android - 在 ImageView (Parse.com) 中加载图像

ios - 使用 Parse 在后台下载多个文件

ios - Swift 2.0 随机 EXC_BAD_ACCESS