【问题标题】:Multiple Tableviews on one page for ipad application用于 ipad 应用程序的一页上的多个 Tableview
【发布时间】:2016-01-20 10:43:09
【问题描述】:

现在正在处理一个应用程序,我希望实现一个双重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
}

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

【问题讨论】:

  • 你的 numberOfSection 和 numberOfRows:inSection 方法在哪里?
  • 感谢您检查 LucaD,但我认为下面的答案应该根据我所看到的情况起作用 :) 如果我还有其他问题,我会尝试在这篇文章中进一步提问,这似乎是有道理的。跨度>

标签: swift uitableview swift2


【解决方案1】:

您可以通过以下方式实现:

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
}

【讨论】:

  • 我认为这正是我正在寻找的 :) 看起来我的问题是我实际上没有正确设置 var,也没有在第二个 tableview 部分中执行 numberOfRows!谢谢 如果我还有其他问题,我会在这里问!
  • @cruelty 当然!不要忘记为答案投票;)
猜你喜欢
  • 2013-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多