Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView逐个单元格更改图像和标题位置_Ios_Swift_Uitableview - Fatal编程技术网

Ios UITableView逐个单元格更改图像和标题位置

Ios UITableView逐个单元格更改图像和标题位置,ios,swift,uitableview,Ios,Swift,Uitableview,我想知道是否有任何可能的方法来创建具有这种样式的表视图: 我有一个包含标题和图像值的字典,我需要创建一个单元格,右一个图像/左一个标题,下一个反之亦然。我们怎样才能做到这一点 创建一个单元格,左右各有2个图像和2个标签 当您转到左侧图像时,此时将隐藏与标签中相同的右侧图像。 细胞 视图控制器 extension ViewController:UITableViewDataSource,UITableViewDelegate { func tableView(_ tableView: UI

我想知道是否有任何可能的方法来创建具有这种样式的表视图:

我有一个包含标题和图像值的字典,我需要创建一个单元格,右一个图像/左一个标题,下一个反之亦然。我们怎样才能做到这一点

创建一个单元格,左右各有2个图像和2个标签 当您转到左侧图像时,此时将隐藏与标签中相同的右侧图像。 细胞

视图控制器

extension ViewController:UITableViewDataSource,UITableViewDelegate
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 120
    }
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 120
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableViewCell", for: indexPath) as? TestTableViewCell
        if (indexPath.row + 1) % 2 == 0 {
            cell?.configure_cell(left: true)
        } else {
            cell?.configure_cell(left: false)
        }

        return cell!
    }
}

实际上有很多方法可以做到这一点:

创建2个单元格。有两个单元格,如OddTableViewCell和EvenTableViewCell。您可以选择在cellForRow方法中使用的索引路径,如:

extension ViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if(indexPath.row%0 == 0) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "EvenTableViewCell", for: indexPath) as! EvenTableViewCell
            cell.model = dataArray[indexPath.row]
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "OddTableViewCell", for: indexPath) as! OddTableViewCell
            cell.model = dataArray[indexPath.row]
            return cell
        }
    }

}
有一个单元格但视图重复,因此有2个标签和2个图像视图。然后根据需要隐藏它们:

class MyCell: UITableViewCell {

    @IBOutlet private var leftImageView: UIImageView?
    @IBOutlet private var rightImageView: UIImageView?

    @IBOutlet private var leftLabel: UILabel?
    @IBOutlet private var rightLabel: UILabel?

    var userImage: UIImage? {
        didSet {
            refresh()
        }
    }
    var userName: String? {
        didSet {
            refresh()
        }
    }
    var imageOnLeft: Bool = false {
        didSet {
            refresh()
        }
    }

    func refresh() {
        leftImageView?.image = imageOnLeft ? userImage : nil
        leftImageView?.isHidden = !imageOnLeft
        rightImageView?.image = imageOnLeft ? nil : userImage
        rightImageView?.isHidden = imageOnLeft

        leftLabel?.text = imageOnLeft ? nil : userName
        leftLabel?.isHidden = imageOnLeft
        rightLabel?.text = imageOnLeft ? userName : nil
        rightLabel?.isHidden = !imageOnLeft
    }

}
具有堆栈视图的单个单元格。将标签和图像视图添加到堆栈视图中。您可以在堆栈视图中更改项目的顺序。可以找到一些有希望的答案。其余的应该与第二个解决方案非常相似


四,。您还可以使用集合视图,并使用标签单元格和图像单元格。

是的,您可以使用表格视图来实现您的要求。您需要遵循以下步骤

方法1:

创建两个表格视图单元XIB,一个带有左侧标签和右侧图像,另一个带有左侧图像和右侧图像。 保持已创建的两个XIB的类相同,但标识符不同。 在表视图中,cellforrowatinexpath方法实现以下逻辑

extension ViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datasourceArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if(indexPath.row % 0 == 0) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "RightLabelTableViewCell", for: indexPath) as! CustomTablViewCell
            cell.model = datasourceArray[indexPath.row]
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "LeftLabelTableViewCell", for: indexPath) as! CustomTablViewCell
            cell.model = datasourceArray[indexPath.row]
            return cell
         }
       }
     }
注意:您可以将一个类用于具有不同 标识符并相应地设计您的xib

方法2:

翻转表视图单元格的内容视图,使其在UI中交换

将以下代码添加到cellForRowAtIndexPath中,并添加其中的else部分,因为行的单元格可能会因解列而表现异常:

 extension UIView {
    /// Flip view horizontally.
    func flipX() {
        transform = CGAffineTransform(scaleX: -transform.a, y: transform.d)
    }
 }
用法:

不要忘记在cellForRowAt方法中添加else部分。

您可以通过以下方式通过setAffineTransform完成:

•使用一个左有图像右有标签的原型单元构建tableView •然后执行以下操作:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellIdentifier", for: indexPath) as! YourTableViewCell

    if (indexPath.row % 2 == 0) {
        cell.contentView.layer.setAffineTransform(CGAffineTransform(scaleX: -1, y: 1))
        cell.YourImage.layer.setAffineTransform(CGAffineTransform(scaleX: -1, y: 1))
        cell.YourLabel.layer.setAffineTransform(CGAffineTransform(scaleX: -1, y: 1))
    }

    // do what ever you want ...

    return cell
}

此外,最好的解决方案是定义2个原型单元,但在您的情况下,这是实现目标的一种复杂而快速的方法。

那么我如何动态创建它呢?单元格0右-左-单元格1左-右-重复这个?你确定等一下吗minutes@Mc.Lover现在检查如果我是你,我会使用UICollectionView。你只能从一个cell@DivyeshGondaliya正如第一句所说,实际上有很多方法可以做到这一点。我的回答中解释了其中的4个。很难从你的评论中看出,但我想你相信有一个细胞总比有两个好。至少从维护的角度来看似乎是这样,但从表视图的工作方式和排队列的工作方式来看,实际上最好有两个排队列标识符。我认为干净的解决方案是使用两个不同的原型单元。但是,从性能的角度来看,您会推荐哪一种?A在公共原型单元上使用setAffineTransform还是B 2不同的原型单元上使用setAffineTransform?
cell.contentView.flipX()
cell.yourImage.flipX()
cell.youImageName.flipX()
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellIdentifier", for: indexPath) as! YourTableViewCell

    if (indexPath.row % 2 == 0) {
        cell.contentView.layer.setAffineTransform(CGAffineTransform(scaleX: -1, y: 1))
        cell.YourImage.layer.setAffineTransform(CGAffineTransform(scaleX: -1, y: 1))
        cell.YourLabel.layer.setAffineTransform(CGAffineTransform(scaleX: -1, y: 1))
    }

    // do what ever you want ...

    return cell
}