swift - iPad 应用程序的一页上有多个 Tableview

标签 swift uitableview swift2

现在正在开发一个应用程序,我希望实现一个双tableview,它根据用户撤回的数据将用户信息分成两个单独的tableview,但我现在陷入困境。我已经尝试找到一种方法来做到这一点有一段时间了,但是我在与此相关的内容中找到的大多数页面并不完全是我正在寻找的内容。我确保所有内容都正确连接以使 tableview 生成自定义单元格,并且所有内容都有一个类,但我仍然没有 tableviewcells' 出现,可能是由于这个区域代码,但我不确定如何进展。我所在位置的示例如下:

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

    let entityDeckDescription = NSEntityDescription.entityForName("Deck", inManagedObjectContext: context)

    var cell = UITableViewCell()

    if tableView == otherCardList {

        let cell: cardDetails = otherCardList.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! cardDetails

        var cardCount = 0
        var myDeckCards: DeckCards?
        let listed = frc.objectAtIndexPath(indexPath) as! Cards
        cell.cardCount.text = ""

        let deckCardsSet = listed.cardselections
        print("The set of DeckCards for that Card is \(deckCardsSet.count)")
        for eachDeckCard in listed.cardselections{
            let myDeckCard = eachDeckCard as! DeckCards
            if myDeckCard.cardsstored == entityDeckDescription {
                // There is already a DeckCard object for this Card and currentDeck
                myDeckCards = eachDeckCard as? DeckCards
                cardCount = (myDeckCards?.numberSelected!.integerValue)!
                if cardCount == 0 {
                    cell.cardCount.text = ""
                } else {
                    cell.cardCount.text = "" + cardCount.description
                }
            }
        }

        switch (listed.cardType) {
        case 1:
            cell.cardAttack?.text = "*"
            cell.cardAttack?.textColor = UIColor.redColor()
            cell.cardHealth?.text = "*"
            cell.heathLabel?.textColor = UIColor(red: 0x22/255, green: 0x63/255, blue: 0x29/255, alpha: 1.0)
            cell.cardHealth?.textColor = UIColor(red: 0x22/255, green: 0x63/255, blue: 0x29/255, alpha: 1.0)
            cell.cardType?.text = "Spell"

        case 2:
            cell.cardAttack?.text = listed.attack.stringValue
            cell.heathLabel?.textColor = UIColor.blackColor()
            cell.cardHealth?.textColor = UIColor.blackColor()
            cell.cardHealth?.text = listed.health.stringValue
            cell.cardType?.text = "Weapon"
            cell.cardAttack?.text = listed.attack.stringValue

        default:
            cell.cardAttack?.text = listed.attack.stringValue
            cell.heathLabel?.textColor = UIColor(red: 0x22/255, green: 0x63/255, blue: 0x29/255, alpha: 1.0)
            cell.cardAttack?.textColor = UIColor.redColor()
            cell.cardHealth?.text = listed.health.stringValue
            cell.cardHealth?.textColor = UIColor(red: 0x22/255, green: 0x63/255, blue: 0x29/255, alpha: 1.0)
            cell.cardType?.text = "Minion"

        }
        cell.cardName?.text = listed.name as String
        cell.cardCost?.text = listed.cost.stringValue
        switch (listed.rarity) {
        case 1:
            cell.cardRarity?.text = "Legendary"
            cell.cardRarity?.textColor = UIColor.orangeColor()
        case 2:
            cell.cardRarity?.text = "Epic"
            cell.cardRarity?.textColor = UIColor.purpleColor()

        case 3:
            cell.cardRarity?.text = "Rare"
            cell.cardRarity?.textColor = UIColor.blueColor()

        case 4:
            cell.cardRarity?.text = "Common"
            cell.cardRarity?.textColor = UIColor.grayColor()

        default:
            cell.cardRarity?.text = "Starter"
            cell.cardRarity?.textColor = UIColor.blackColor()
        }
        switch (listed.cardClass) {
        case 1:
            cell.cardName?.textColor = UIColor(red: 0xbe/255, green: 0x23/255, blue: 0x0f/255, alpha: 1.0)
        case 2:
            cell.cardName?.textColor = UIColor.blueColor()
        case 3:
            cell.cardName?.textColor = UIColor(red: 0x75/255, green: 0x47/255, blue: 0x19/255, alpha: 1.0)

        case 4:
            cell.cardName?.textColor = UIColor(red: 0xFF/255, green: 0x80/255, blue: 0x00/255, alpha: 1.0)

        case 5:
            cell.cardName?.textColor = UIColor(red: 0x22/255, green: 0x63/255, blue: 0x29/255, alpha: 1.0)

        case 6:
            cell.cardName?.textColor = UIColor.brownColor()

        case 7:
            cell.cardName?.textColor = UIColor(red: 0x5E/255, green: 0x03/255, blue: 0x8F/255, alpha: 1.0)

        case 8:
            cell.cardName?.textColor = UIColor(red: 0x01/255, green: 0xA9/255, blue: 0xDB/255, alpha: 1.0)

        case 9:
            cell.cardName?.textColor = UIColor.magentaColor()

        default:
            cell.cardName?.textColor = UIColor.blackColor()
        }
        return cell

    } else if tableView == classCardList {

        let cell: cardDetails = classCardList.dequeueReusableCellWithIdentifier("classcell", forIndexPath: indexPath) as! cardDetails

        var cardCount = 0
        var myDeckCards: DeckCards?
        let listed = classfrc.objectAtIndexPath(indexPath) as! Cards
        cell.cardCount.text = ""

        let deckCardsSet = listed.cardselections
        print("The set of DeckCards for that Card is \(deckCardsSet.count)")
        for eachDeckCard in listed.cardselections{
            let myDeckCard = eachDeckCard as! DeckCards
            if myDeckCard.cardsstored == entityDeckDescription {
                // There is already a DeckCard object for this Card and currentDeck
                myDeckCards = eachDeckCard as? DeckCards
                cardCount = (myDeckCards?.numberSelected!.integerValue)!
                if cardCount == 0 {
                    cell.cardCount.text = ""
                } else {
                    cell.cardCount.text = "" + cardCount.description
                }
            }
        }

        switch (listed.cardType) {
        case 1:
            cell.cardAttack?.text = "*"
            cell.cardAttack?.textColor = UIColor.redColor()
            cell.cardHealth?.text = "*"
            cell.heathLabel?.textColor = UIColor(red: 0x22/255, green: 0x63/255, blue: 0x29/255, alpha: 1.0)
            cell.cardHealth?.textColor = UIColor(red: 0x22/255, green: 0x63/255, blue: 0x29/255, alpha: 1.0)
            cell.cardType?.text = "Spell"

        case 2:
            cell.cardAttack?.text = listed.attack.stringValue
            cell.heathLabel?.textColor = UIColor.blackColor()
            cell.cardHealth?.textColor = UIColor.blackColor()
            cell.cardHealth?.text = listed.health.stringValue
            cell.cardType?.text = "Weapon"
            cell.cardAttack?.text = listed.attack.stringValue

        default:
            cell.cardAttack?.text = listed.attack.stringValue
            cell.heathLabel?.textColor = UIColor(red: 0x22/255, green: 0x63/255, blue: 0x29/255, alpha: 1.0)
            cell.cardAttack?.textColor = UIColor.redColor()
            cell.cardHealth?.text = listed.health.stringValue
            cell.cardHealth?.textColor = UIColor(red: 0x22/255, green: 0x63/255, blue: 0x29/255, alpha: 1.0)
            cell.cardType?.text = "Minion"

        }
        cell.cardName?.text = listed.name as String
        cell.cardCost?.text = listed.cost.stringValue

        switch (listed.rarity) {
        case 1:
            cell.cardRarity?.text = "Legendary"
            cell.cardRarity?.textColor = UIColor.orangeColor()

        case 2:
            cell.cardRarity?.text = "Epic"
            cell.cardRarity?.textColor = UIColor.purpleColor()

        case 3:
            cell.cardRarity?.text = "Rare"
            cell.cardRarity?.textColor = UIColor.blueColor()
        case 4:
            cell.cardRarity?.text = "Common"
            cell.cardRarity?.textColor = UIColor.grayColor()

        default:
            cell.cardRarity?.text = "Starter"
            cell.cardRarity?.textColor = UIColor.blackColor()
        }
        switch (listed.cardClass) {
        case 1:
            cell.cardName?.textColor = UIColor(red: 0xbe/255, green: 0x23/255, blue: 0x0f/255, alpha: 1.0)

        case 2:
            cell.cardName?.textColor = UIColor.blueColor()

        case 3:
            cell.cardName?.textColor = UIColor(red: 0x75/255, green: 0x47/255, blue: 0x19/255, alpha: 1.0)

        case 4:
            cell.cardName?.textColor = UIColor(red: 0xFF/255, green: 0x80/255, blue: 0x00/255, alpha: 1.0)

        case 5:
            cell.cardName?.textColor = UIColor(red: 0x22/255, green: 0x63/255, blue: 0x29/255, alpha: 1.0)

        case 6:
            cell.cardName?.textColor = UIColor.brownColor()

        case 7:
            cell.cardName?.textColor = UIColor(red: 0x5E/255, green: 0x03/255, blue: 0x8F/255, alpha: 1.0)
        case 8:
            cell.cardName?.textColor = UIColor(red: 0x01/255, green: 0xA9/255, blue: 0xDB/255, alpha: 1.0)
        case 9:
            cell.cardName?.textColor = UIColor.magentaColor()
        default:
            cell.cardName?.textColor = UIColor.blackColor()

        }
    }

    return cell
}

我相当确定这是导致问题的代码区域,但如果你们认为它在其他地方,也许我们可以找出问题的根源。我对于编码还是个新手,并且总是尽我所能地学习。

最佳答案

您可以通过以下方式实现此目的:

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

    if tableView == tableOutletHere {
        // initialize cell here

        returnCell = cell
    }

    if tableView == tableOutletHere {
        // initialize cell here

        returnCell = cell
    }

    ...
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var returnValue: Int = 0

    if tableView == tableOutletHere {
        returnValue = array1.count
    }

    if tableView == tableOutletHere {
        returnValue = array2.count
    }

    ...

    return returnValue
}

关于swift - iPad 应用程序的一页上有多个 Tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34897786/

相关文章:

swift - UIStatusBar不会消失

xcode - 静态 UITableView 在内容 View 中不显示带有 subview (UIButton、UISlider 等)的单元格

ios - tableView 中的多个继承类

swift - 如何在 Swift 中将泛型和 Any.Type 与函数一起使用

ios - 将 Google Analytics 添加到项目后 CoreData 方法出错

ios - 将字符串转换为 NSURL 返回 nil

ios - 在 Swift 4 中使用对象映射器解析嵌套对象

ios - 在 NavigationBar 上带有隐藏 TableView 的 UISearchBar

ios - 如何在 UITextField 中添加图像和文本作为 swift 居中的占位符

ios - 无法获取组成员 (XMPP)