【问题标题】:Inserting rows in UITableView upon click单击时在 UITableView 中插入行
【发布时间】:2015-11-20 06:15:27
【问题描述】:

点击UIButton 时,我无法向UITableView 添加行。

我有两个自定义单元格 xib - 一个包含 UILabel,另一个包含 UIButton

表格单元格的数据从两个字典(answersmainlinesmain)加载。

这是UITableView主要功能的代码:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.linesmain["Audi"]!.count + 1
}


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

    if(indexPath.row < 3){
        var cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell

        cell.lblCarName.text = linesmain["Audi"]![indexPath.row]

        return cell

    } else  {
      var celle:vwAnswers = self.tableView.dequeueReusableCellWithIdentifier("cell2") as! vwAnswers
        celle.Answer.setTitle(answersmain["Good car"]![0], forState:UIControlState.Normal)

                return celle
    }}

我在这里放什么?

@IBAction func option1(sender: UIButton) {

// I need to add rows to the uitableview from two dictionaries into two different xibs 

}

【问题讨论】:

    标签: ios swift uitableview dictionary custom-cell


    【解决方案1】:

    您需要修改linesmainanswersmain,向它们添加数据,然后调用[self.tableView reloadData]

    如果你提取linesmain["Audi"]answersmain["Good car"]并将它们保存到不同的可变数组中并修改它们会更好。

    您需要在func option1 中执行此操作。

    【讨论】:

      【解决方案2】:

      你应该看看文档here

      有一个称为insertRowsAtIndexPaths:withRowAnimation:UITableView 方法在指定的indexPath 处插入行。

      【讨论】:

        【解决方案3】:

        你可以做下一个:

        var showingAll = false
        
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return showingAll ? self.linesmain["Audi"]!.count + 1 : 0
        }
        
        @IBAction func option1(sender: UIButton) {
            showingAll = true
        
            tableView.beginUpdates()
        
            let insertedIndexPathRange = 0..<self.linesmain["Audi"]!.count + 1
            var insertedIndexPaths = insertedIndexPathRange.map { NSIndexPath(forRow: $0, inSection: 0) }
            tableView.insertRowsAtIndexPaths(insertedIndexPaths, withRowAnimation: .Fade)
        
            tableView.endUpdates()
        }
        

        【讨论】:

        • 尝试了代码。实际上,它返回的是空的 tableview。
        • 点击按钮后的空tableview?
        • 启动应用后
        • 除非我将 return showingAll ? self.linesmain["Audi"]!.count + 1 : 0 最后一个零更改为任何更大的 int 值。然后它在tableView.endUpdates( )@sgl0v 上崩溃
        • 崩溃日志是incorrect checksum for freed object - object was probably modified after being freed@sgl0v
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多