为什么在我单击项目时,标题和图像总是在变化?

时间:2019-05-29 06:36:32

标签: ios swift uitableview

每次单击项目时,都会显示一个带有导航栏的新控制器。但是不幸的是,图像和标题会更改其顺序,并且可以像here一样复制它们。

这里是视图控制器的code

func showList() {
    let actionsKeys = Array(actionsDir.keys)
    let iconStrings = Array(iconsDir.values)
    let items = Observable.just(actionsKeys)
    _ = items.bind(to:collectionView.rx.items) { (tableView, row, structured) in
        do {
            let cell = tableView.dequeueReusableCell(withReuseIdentifier: "ProjectOverviewCell", for: IndexPath(row: row, section: 0)) as! ProjectOverviewCell
            cell.titelLabel.text = actionsKeys[row]
            cell.imageView.image = UIImage(named: iconStrings[row])
            cell.imageView.tintColor = UIColor.darkGray
            return cell
        }
    }

    _ = collectionView.rx.modelSelected(String.self).subscribe(onNext: { pnc in
        let storyBoard = UIStoryboard(name:"Main", bundle:nil)
        let newController = storyBoard.instantiateViewController(withIdentifier: self.actionsDir[pnc]!)
        newController.navigationItem.title = pnc
        self.containerView.addSubview(newController.view)  
    })

订单应确定。

1 个答案:

答案 0 :(得分:2)

使用表,图标和控制器名称作为属性创建一个结构,并将该结构存储在数组中

struct Action {
    let label: String
    let control: String
    let icon: String
}

let actions = [Action(label: "Pläne", control: "PlansViewController2", icon: "plancorrection"), 
               Action(label: "Dokumentationen", control: "DocumentationListViewCtrl", icon: "list"), ...]

然后从同一数组中获取所有需要的值

cell.titelLabel.text = actions[row].label
cell.imageView.image = UIImage(named: actions[row].icon)
cell.imageView.tintColor = UIColor.darkGray

为视图控制器获取正确的标识符

if let action = actions.first(where: {$0.label == pnc}) {
    let newController = storyBoard.instantiateViewController(withIdentifier: action.control)
} else {
    //some error handling here
}