tableview单元格子scrollview没有水平滚动

时间:2018-11-06 17:07:16

标签: ios swift uitableview

嗨,我正在使用UITableView视图,在UITableViewCell内部,我使用的是scrollview,它将水平显示图像,并且可以使用虚拟数组值正常工作,从服务器获取数据后,我将在这种情况下,重新加载UITableView可以正常工作,但是由于UIScrollViewDispatchQueue.main.async的内容没有水平滚动。我不确定主线程是否会影响scrollview滚动。

这是我的cellForRowAt方法:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell:PostImageCell = tablView.dequeueReusableCell(withIdentifier: "PostImageCell") as! PostImageCell
        cell.imageScrollView?.delegate = self
        let Padding:CGFloat = 10
        var Offset:CGFloat = 5
        DispatchQueue.main.async {
            let imagarray = self.UserDatas?.posts.app[indexPath.row].images

            for image in imagarray ?? ["string"] {
                let cardView: UIImageView = UIImageView()
                cardView.frame = CGRect(x: Offset, y: CGFloat(Padding), width: 150, height:                cell.imageScrollView.frame.height-10)
                cardView.backgroundColor = UIColor.gray
                cardView.layer.cornerRadius = 5
                cardView = UIImageView(image: image)
                cardView.contentMode = .scaleAspectFit
                [![cell.imageScrollView?.addSubview(cardView)
                Offset = Offset + CGFloat(Padding) + cardView.frame.size.width
            }
        }
        cell.imageScrollView?.contentSize = CGSize(width: Offset, height: cell.imageScrollView.frame.height)

        cell.selectionStyle = .none
        return cell
    }

It's my view

2 个答案:

答案 0 :(得分:0)

在代码中使用以下代码段,希望对您有所帮助,

cardView.frame = CGRect(x: (CGFloat(i) * 150) + 5,
                                        y: CGFloat(Padding),
                                        width: 140,
                                        height: cell.imageScrollView.frame.height-10)


Offset = CGFloat(imagarray.count) * cardView.frame.size.width

因为,偏移量分配导致滚动视图内容大小的冲突。

答案 1 :(得分:0)

以下代码对我有用。也许后台下载会阻止UI。看一眼 DispatchQueue.main.async {             ...             self.tv.reloadData()         } 用于更新ui。

class PostImageCell : UITableViewCell {
var imageScrollView : UIScrollView!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {     
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    imageScrollView = UIScrollView()
    imageScrollView.canCancelContentTouches = true
    self.contentView.addSubview(imageScrollView)
    imageScrollView.frame.size = CGSize(width: self.contentView.frame.width,height: 150.0)
}
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var myimages : [UIImage] = []
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}
var tv : UITableView!
let PostImageCellid = "PostImageCell"

override func viewDidLoad() {
    super.viewDidLoad()

    //My dummy images
    for i in 0...5 {
        let image = UIImage().WithSize(size: CGSize(width: 150.0, height: 150.0))
        myimages.append(image.drawText(text: String(i)))
    }

    tv = UITableView()
    tv.register(PostImageCell.self, forCellReuseIdentifier: self.PostImageCellid)
    tv.dataSource = self
    tv.delegate = self
    self.view.addSubview(tv)

    //Now download a image in background
    ImageLoader.downloadImage(urlstr: "https://www.google.de/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png") { image in
        DispatchQueue.main.async {
            self.myimages[0] = image
            self.tv.reloadData()
        }
    }
}

override func viewDidLayoutSubviews() {
    tv.frame = view.frame
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    print("Cellforrow")
    let cell:PostImageCell = tableView.dequeueReusableCell(withIdentifier: "PostImageCell") as! PostImageCell
    cell.imageScrollView?.delegate = self
    let Padding:CGFloat = 10
    var Offset:CGFloat = 5
    for image in myimages  {
        let cardView: UIImageView = UIImageView()
        cardView.frame = CGRect(x: Offset, y: CGFloat(Padding), width: 150, height: cell.imageScrollView.frame.height-10)
        cardView.backgroundColor = UIColor.gray
        cardView.layer.cornerRadius = 5
        cardView.image = image
        cardView.contentMode = .scaleAspectFit
        cell.imageScrollView?.addSubview(cardView)
        Offset = Offset + CGFloat(Padding) + cardView.frame.size.width
    }
    cell.imageScrollView?.contentSize = CGSize(width: Offset, height: cell.imageScrollView.frame.height)
    cell.selectionStyle = .none
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 150.0
}

}

还有一些用于虚拟图像和从Google下载图像的帮助程序类:

lass ImageLoader {  
static func downloadImage(urlstr : String, complete: @escaping ((UIImage)->Void)){
    guard let url = URL(string: urlstr) else { return }
    let urlRequest = URLRequest(url: url)
    let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
        guard error == nil else { return }
        guard let data = data else { return }
        guard let image = UIImage(data: data) else { return }
        complete(image)
    }
    task.resume()
}

}

extension UIImage {
func  drawText(text: String) -> UIImage {
    let color = UIColor.white
    let font = UIFont(name: "Helvetica Bold", size: 16)!
    let scale = UIScreen.main.scale
    UIGraphicsBeginImageContextWithOptions(self.size, false, scale)
    defer {
        UIGraphicsEndImageContext()
    }
    let textFontAttributes = [
        NSAttributedString.Key.font: font,
        NSAttributedString.Key.foregroundColor: color,
        ] as [NSAttributedString.Key : Any]
    self.draw(in: CGRect(origin: CGPoint.zero, size: self.size))

    let rect = CGRect(origin: .zero, size: self.size)
    text.draw(in: rect, withAttributes: textFontAttributes)

    guard let newImage = UIGraphicsGetImageFromCurrentImageContext() else { return self }
    return newImage
}

func WithSize(size: CGSize, color: UIColor = UIColor.clear) -> UIImage {
    let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
    let scale = UIScreen.main.scale
    UIGraphicsBeginImageContextWithOptions(size, false, scale)
    defer { UIGraphicsEndImageContext() }
    color.set()
    UIRectFill(rect)

    guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return self }
    return image
}

}