的UITableView通过细胞改变图像和标题位置细胞

问题描述 投票:-2回答:4

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

enter image description here

我有一本字典包含标题和图像值,我需要建立一个小区的一个图片,右键/标题 - 左和明年反之亦然。如何才能做到这样呢?

ios swift uitableview
4个回答
0
投票

您可以通过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:

  • 创建两个表视图细胞厦门国际银行的一个与左侧标签和右侧图像,第二个是与左侧图像和右侧图像。
  • 保持同一个类你已经创建两个XIB的,但有不同的标识符。
  • 在您的表视图cellForRowAtIndexPath方法实现以下逻辑。 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 } } }

注意:您可以使用TableViewCell一个类不同的标识符,并相应地设计您的厦门国际银行的。

方法2:

翻转以这样一种方式,他们会在你的用户界面交换你的表视图单元格的内容视图。

添加以下代码到你的cellForRowAtIndexPath也可以增加它的其他部分,因为一排电池可以表现古怪,因为dequeing的:

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

用法:

cell.contentView.flipX()
cell.yourImage.flipX()
cell.youImageName.flipX()

不要忘了在cellForRowAt方法添加其他部分。


-1
投票

有这样做的其实很多:

  1. 创建2个细胞。有2个细胞像OddTableViewCellEvenTableViewCell。你可以用它来像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个标签和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 } }
  3. 具有堆叠视图中的单个细胞。添加一个标签和图像视图到堆栈视图。您可以更改堆栈视图项目的顺序。一些有前途的答案可以发现here。其余的应该是相当类似的第二个解决方案。

(4)您也可以只使用一个集合视图,并有一个标签细胞和图像细胞。


-1
投票
  • 2图像和2标签的左侧和右侧创建一个单元
  • 当你去到左侧图像当时躲右图像相同的标签。

细胞

import UIKit

class TestTableViewCell: UITableViewCell {

    @IBOutlet weak var lbl_left: UILabel!
    @IBOutlet weak var lbl_right: UILabel!
    @IBOutlet weak var img_right: UIImageView!
    @IBOutlet weak var img_left: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func configure_cell(left:Bool)
    {
        if left{
            img_left.isHidden = true
            img_right.isHidden = false
            lbl_left.isHidden = false
            lbl_right.isHidden = true
            self.img_right.image = UIImage(named: "testimg")
        }else{
            img_left.isHidden = false
            img_right.isHidden = true
            lbl_left.isHidden = true
            lbl_right.isHidden = false
            self.img_left.image = UIImage(named: "testimg")
        }
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

视图控制器

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!
    }
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.