ios - UITableViewCell 没有正确更新 swift 2.2

标签 ios swift uitableview

在我的应用程序中,我创建了两个自定义表格 View 单元格。

我现在面临的问题是第二个表格 View 单元格仅使用数组的最后一个元素进行更新。

cellForRowAtIndexpath 数组元素中显示正常。

考虑 [ "Value1", "Value2"] 是我的数组。在 tableView 中,只有 value2 显示在两个单元格中。

var title = ["value1","value2"]

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

        let row = indexPath.row

        let x = Id[indexPath.row]

        if x == 0{

        let cell1 = tableView.dequeueReusableCellWithIdentifier("Cell1", forIndexPath: indexPath) as! MyCell1

            return cell1
        }
        else{

      let cell2 = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as! MyCell2

            for index in 0..<myArray.count{
        cell2.titleButton.setTitle(title[index],forState:UIControlState.Normal)
            }
            return cell2
        }
    }

我被困在这里,我们将不胜感激您的帮助。

最佳答案

以下是解决方案,它超出范围的原因是因为每次向下滚动时调用 cellforrowAtIndexPath 时单元格被取消队列时值增加(因为一些单元格不可见并且当我们向下滚动时这些单元格被取消队列): -

    var name = ["HouseBolo","HouseBolo1","HouseBolo2","HouseBolo3"]
    var propertyVal:Int = 0
    var projectVal:Int = 0
    var type = ["Apartment","Villa","Home","Flat","Plot"]
    var arrangedData = [String]()
    var flatId = [0,1,2,0,0]

    override func viewDidLoad() {
        super.viewDidLoad()
        // I want the Expected Output in Tableview Cell is
        // 1. Apartment 2. HouseBolo 3. HouseBolo1 4. Villa 5. Home


        for item in flatId {
            if item == 0 {
                arrangedData.append(type[propertyVal])
                propertyVal+=1
            }
            else {
                arrangedData.append(name[projectVal])
                projectVal+=1
            }
        }
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let flatDetails = flatId[indexPath.row]

        // For Property Cell
        if flatDetails == 0{
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell1", forIndexPath: indexPath) as? PropertyCell

            if(cell != nil) {
                cell!.pType.text = arrangedData[indexPath.row]
            }
           return cell!
        }
        // For Project Cell
        else {

            let cellan = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as? ProjectCell

            if(cellan != nil)  {
             cellan!.projectName.setTitle(arrangedData[indexPath.row], forState: UIControlState.Normal)
            }
            return cellan!
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

关于ios - UITableViewCell 没有正确更新 swift 2.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37384402/

相关文章:

ios - rawValue 属性在哪里?

ios - 带有网络管理面板的移动应用程序

swift - 在 swift 字典中引用为键

ios - 在 iOS 中按日期排序

swift - 由于编译器中的 'internal' 保护级别,无法访问框架 init 中的公共(public)结构

ios - Swift 中的 UITableView – 未知异常

ios - tableview 在使用 xib 单元格时发现 nil

ios - 基于按钮按下 Swift 在 sameTableViewController 中加载数据

ios - mkannotation View 中的按钮操作不起作用?

ios - 使用 sprite 套件创建 2d 波纹效果