【问题标题】:Image change in UITableViewCell using Segmented control in another View在另一个视图中使用分段控件在 UITableViewCell 中更改图像
【发布时间】:2016-08-29 18:52:01
【问题描述】:

如何使用 swift 在另一个视图控制器中使用分段控制更改 UITableViewCell 中设置的 IBOutlet 图像?

UITableViewCell.swift

class UserCell: UITableViewCell {

    @IBOutlet var imageDisplayIcon: UIImageView!

视图控制器:

@IBAction func btnClicked(sender: AnyObject) {

    if txtSex.selectedSegmentIndex == 0{

        var imagePicView: UserCell = UserCell()

        imagePicView.imageDisplayIcon.image = UIImage(named: "BoyIcon.png")

        //imageDisplay.image = UIImage(named: "BoyIcon.png")

        print("male")

    }

    if txtSex.selectedSegmentIndex == 1{


        var imagePicView: UserCell = UserCell()

        imagePicView.imageDisplayIcon.image = UIImage(named: "GirlIcon1.png")

        //imageDisplay.image = UIImage(named: "GirlIcon1.png")

        print("female")

    }

我收到一个错误提示

"fatal error: unexpectedly found nil while unwrapping an Optional value."

我该怎么办??

【问题讨论】:

  • 您的viewController 中是否添加了tableView?如果是,您需要更改cellForRowAtIndexPath 中的图像。

标签: ios uitableview uisegmentedcontrol


【解决方案1】:

我想你有一个UITableView

1.在动作中重新加载表格数据

@IBAction func btnClicked(sender: AnyObject) {
    tableView.reloadData()
}

2。在表格数据源委托中添加图片单元格

func tableView(_ tableView: UITableView,
    cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let imageName = txtSex.selectedSegmentIndex == 0 ? "BoyIcon.png" : "GirlIcon1.png"

    // Load your cell

    cell.imageDisplayIcon.image = UIImage(named: imageName)

    return cell
}

【讨论】:

  • "@btnClicked" 操作不会在 tableView 的 viewController 中发生。所以我认为“tableView,reloadData”再次显示错误。
  • @Jenita_Alice4Real 如果您在另一个视图控制器中有 tableView,您应该调用该视图控制器的函数来重新加载表或将 tableview 公开为公共属性。
  • 我编写代码的视图控制器不是我的表的视图。这是另一个视图,用户可以在男性和女性之间进行选择,以便单元格中的图像相应地发生变化。
  • 如果段视图在另一个流中,您可以存储 selectedSegmentIndex,当您返回 tableview 时,您可以使用存储的 selectedSegmentIndex 值重新加载数据
【解决方案2】:

在设置单元格的图像视图使其影响到 tableview 后,您应该在段的单击操作上重新加载您的 tableview

您需要相应地管理您的cellforrow。在您的cellforrow 数据源方法中,应该有这样的条件:如果所选片段是男性,则图像应该是男性,反之亦然!

【讨论】:

    【解决方案3】:

    您应该在 UserCell 和 ViewController 之间使用“delegate”。这是最好的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-02
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      相关资源
      最近更新 更多