Swift UITableviewcell无需重用

时间:2014-12-05 15:12:46

标签: ios iphone uitableview swift reuseidentifier

我在另一个tableView里面的tableView里面创建了一个tableView。问题是当重用单元格时,另一个tableView中的tableView被破坏

我通常会收到该错误

  

无效更新:第0部分中的行数无效   更新(1)后必须包含在现有部分中的行   等于之前该部分中包含的行数   update(2),加上或减去插入或删除的行数   该部分(1个插入,0个删除)和加号或减号的数量   移入或移出该部分的行(0移入,0移出)。'

。如何在没有重用系统的情况下在swift中创建一个单元格?

任何帮助将不胜感激:)

private(set) let kNAME_CELL_FIRST          = "Cell1"
private(set) let kNAME_CELL_SECOND         = "Cell2"
private(set) let kNAME_CELL_THIRD          = "Cell3"

private (set) var indexSelectedFirst      : NSIndexPath!
weak var currentTableViewSecond          : UITableView!    

enum IDTableView : Int {
    case kFIRST         = 0
    case kSECOND        = 1
    case kTHIRD         = 2
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


    switch tableView.tag {
    case IDTableView.kFIRST.rawValue:
        return self.categories.count
    case IDTableView.kSECOND.rawValue:
        return self.getItemSECOND()
    case IDTableView.kTHIRD.rawValue:
        return 1
    default: return 0
    }
}




func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    switch tableView.tag {
    case IDTableView.FIRST.rawValue :
        if self.idSelectedSecond != -1 && self.indexSelectedFirst != nil && indexPath == self.indexSelectedFirst {
            return HEIGHT_CELL_FIRST + (HEIGHT_CELL_SECOND * CGFloat(self.getItemSecond())) + HEIGHT_CELL_THIRD
        }
        else if  self.indexSelectedCat != nil && indexPath == self.indexSelectedCat  {
            return HEIGHT_CELL_FIRST + (HEIGHT_CELL_SECOND * CGFloat(self.getItemSecond()))
        }
        return HEIGHT_CELL_FIRST
    case IDTableView.SECOND.rawValue :
        if self.indexSelectedSecond != nil  && self.indexSelectedSecond == indexPath {
            return HEIGHT_CELL_SECOND + HEIGHT_CELL_THIRD
        } else {
            return HEIGHT_CELL_SECOND
        }
    case IDTableView.THIRD.rawValue:
        return HEIGHT_CELL_THIRD
    default:
        return 0
    }
}


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

    switch tableView.tag {
    case IDTableView.FIRST.rawValue:
        return self.tableViewFirst(tableView, cellForRowAtIndexPath: indexPath)
    case IDTableView.Second.rawValue:
        return self.tableViewSecond(tableView, cellForRowAtIndexPath: indexPath)
    case IDTableView.THIRD.rawValue:
        return self.tableViewThird(tableView, cellForRowAtIndexPath: indexPath)
    default:
        return UITableViewCell()
    }
}



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

    let cell : firstTableViewCell = tableView.dequeueReusableCellWithIdentifier(kNAME_CELL_CAT, forIndexPath: indexPath) as firstTableViewCell

    let item = self.arrFirst[indexPath.row]
    cell.labelName.text                     = item.name
    cell.imageView_.image                   = UIImage(named: "test.jpg")
    cell.imageView_.clipsToBounds           = true
    cell.selectionStyle                     = UITableViewCellSelectionStyle.None;

    if self.indexSelectedFirst != nil && indexPath == self.indexSelectedFirst {
        cell.labelName.textColor = UIColor(rgb: 0xf1d3b6)
        cell.tableViewSecond.hidden = false

    } else {
        cell.labelName.textColor = UIColor(rgb: 0xf1d3b6)
        cell.tableViewSecond.hidden = true
    }
    return cell
}

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

    let cell : SecondTableViewCell = tableView.dequeueReusableCellWithIdentifier(kNAME_CELL_SECOND) as SecondTableViewCell

    let itemParent = self.getSelectedItemFirstArray()!
    let item = itemParent.arraySecond[indexPath.row]

    cell.labelName.text         = item.name
    cell.selectionStyle         = UITableViewCellSelectionStyle.None

    return cell
}


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

    let cell : ThirdTableViewCell = tableView.dequeueReusableCellWithIdentifier(kNAME_CELL_THIRD) as ThirdTableViewCell

    cell.labelKey.text          = "test string"
    cell.selectionStyle         = UITableViewCellSelectionStyle.None

    return cell
}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    switch  tableView.tag {
    case IDTableView.FIRST.rawValue:
        self.tableViewFirst(tableView, didSelectRowAtIndexPath: indexPath)
    default:break
    }
}


func tableViewFirst(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let lastIndexPath = self.indexSelectedFirst

    self.closeCurrentSelectedItemK1 { (over: Bool) -> Void in

        // JUST NEED TO CLOSE THE ITEM
        if lastIndexPath != nil && lastIndexPath == indexPath { return }

        let cell = tableView.cellForRowAtIndexPath(indexPath) as FirstTableViewCell
        self.indexSelectedFirst         = indexPath
        self.currentTableSecond         = cell.tableViewSecond
        cell.tableViewSecond.hidden    = false

        UIView.animateWithDuration(0.6, animations: { () -> Void in
           var indexPaths  = [NSIndexPath]()
           CATransaction.begin()
           CATransaction.setCompletionBlock({ () -> Void in
               self.tableViewFirst.beginUpdates()
               self.tableViewFirst.endUpdates()
           })
        self.currentTableViewSecond.beginUpdates()

        for i in 0...(self.getItemSecond()-1) {
            indexPaths.append(NSIndexPath(forRow: i, inSection: 0))
        }
        self.currentTableViewSecond.insertRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.Bottom)
        self.currentTableViewSecond.endUpdates()
        CATransaction.commit()
        }, completion: { (over: Bool) -> Void in })   
    }
}


func closeCurrentSelectedItemK1(completion : ((Bool)-> Void)) -> Void{

    if self.indexSelectedFirst != nil {

        var indexPaths = [NSIndexPath]()
        UIView.animateWithDuration(0.3, animations: { () -> Void in
            let cell  = self.tableViewFirst.cellForRowAtIndexPath(self.indexSelectedFirst) as FirstTableViewCell

            CATransaction.begin()
            self.currentTableViewSecond.beginUpdates()
                self.currentTableViewSecond.hidden = true

                for i in 0...self.currentTableViewSecond.numberOfRowsInSection(0)-1 {
                    indexPaths.append(NSIndexPath(forRow: i, inSection: 0))
                }
                self.indexSelectedFirst = nil
                /* IF SOME TO REMOVE */
                if indexPaths.count > 0 {
                    self.currentTableViewSecond.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.Top)
                }
                self.currentTableViewSecond.endUpdates()
                CATransaction.setCompletionBlock({ () -> Void in
                    self.tableViewFirst.beginUpdates()
                    self.tableViewFirst.endUpdates()
                    self.currentTableViewSecond    = nil
                self.indexSelectedFirst = nil
              completion(true)
                })
                CATransaction.commit()
    } else {
        completion(true)
    }
}

0 个答案:

没有答案
相关问题