核心数据确实选择了更新文本字段

时间:2015-09-07 13:14:29

标签: swift

我有一个tableview,向用户显示自定义单元格,根据其选择,在其选择中更改text.label。我尝试了各种方法,但使用下面的代码列表并不起作用。我已经尝试了多种方法来完成这项工作。对此发生的情况是,如果我选择所呈现的1个单元格,则选择每10个单元格而不是仅选择一个单元格。我试着用:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var myDeckCards: DeckCards?
    let listed = frc.objectAtIndexPath(indexPath) as! Cards

    // check: is there already a DeckCards object for this Card and this Deck?
    let deckCardsSet = listed.cardselections
    println("The set of DeckCards for that Card is \(deckCardsSet.count)")
    for eachDeckCard in listed.cardselections {
        let myDeckCard = eachDeckCard as! DeckCards
        if myDeckCard.cardsstored == passedDeckObject {
            // There is already a DeckCard object for this Card and currentDeck
            myDeckCards = eachDeckCard as? DeckCards
        }
    }
    if myDeckCards == nil {
        // There is no DeckCard object for this Card and currentDeck
        // So create one...
        myDeckCards = NSEntityDescription.insertNewObjectForEntityForName("DeckCards", inManagedObjectContext: managedObjectContext!) as? DeckCards
        myDeckCards!.cardsselected = listed
        myDeckCards!.cardsstored = passedDeckObject!
    }
    // your code to determine numberSelected here; I'll assume 2!
    cardCount = myDeckCards!.numberSelected.integerValue
    deckCardCount = myDeckCards!.deckcardCount.integerValue
    cardCount = cardCount == 2 ? 0 : cardCount + 1
    println(deckCardCount)
    println(cardCount)
    myDeckCards!.numberSelected = cardCount
    let indexPathUpdate: AnyObject = frc.objectAtIndexPath(indexPath.row)
    let currentCell = cardsListed.cellForRowAtIndexPath(indexPathUpdate) as! firstCardDetails?
      //firstCardDetails customtableviewcellcontroller//
        if currentCell!.selected == true {
            if cardCount == 0 {
                currentCell!.cardCounter.text = "  "
            } else {
                if cardCount == 1 {
                    currentCell!.cardCounter.text = "  1"
                } else {
                    if cardCount == 2 {
                        currentCell!.cardCounter.text = "  2"
                }
            }
        }
    }
}

根据要求,这是cellForRowAtIndexPath func:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell: firstCardDetails = cardsListed.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! firstCardDetails
    let listed = frc.objectAtIndexPath(indexPath) as! Cards

    cell.cardName?.text = listed.name as String
    if listed.cardType.isEqualToValue(1) {
        cell.cardAttack?.text = "*"
    } else {
        if listed.cardType.isEqualToValue(2) {
            cell.cardAttack?.text = "*"
        } else {
    cell.cardAttack?.text = listed.attack.stringValue
    }
    }
    if listed.cardType.isEqualToNumber(1) {
        cell.cardHealth?.text = "*"
    } else {
        if listed.cardType.isEqualToValue(2) {
            cell.cardHealth?.text = "*"
        } else {
    cell.cardHealth?.text = listed.health.stringValue
    }
    }
    cell.cardCost?.text = listed.cost.stringValue

        if listed.cardType.isEqualToNumber(0) {
            cell.cardType?.text = "Minion"
        } else {
            if listed.cardType.isEqualToNumber(1) {
                cell.cardType?.text = "Spell"
            } else {
                if listed.cardType.isEqualToNumber(2) {
                cell.cardType?.text = "Weapon"
            }
            }
    }
    if listed.rarity.isEqualToNumber(1) {
        cell.rarityType?.text = "Legendary"
        cell.rarityType?.textColor = UIColor.orangeColor()
    } else {
        if listed.rarity.isEqualToNumber(2) {
            cell.rarityType?.text = "Epic"
            cell.rarityType?.textColor = UIColor.purpleColor()
        } else {
            if listed.rarity.isEqualToNumber(3) {
                cell.rarityType?.text = "Rare"
                cell.rarityType?.textColor = UIColor.blueColor()
            } else {
                if listed.rarity.isEqualToNumber(4) {
                    cell.rarityType?.text = "Common"
                    cell.rarityType?.textColor = UIColor.grayColor()
                } else {
                    if listed.rarity.isEqualToNumber(5) {
                        cell.rarityType?.text = "Starter"
                        cell.rarityType?.textColor = UIColor.blackColor()
                    }
                }
            }
        }
    }
    if listed.cardClass.isEqualToNumber(1) {
        cell.cardName?.textColor = UIColor(red: 0xbe/255, green: 0x23/255, blue: 0x0f/255, alpha: 1.0)
    } else {
        if listed.cardClass.isEqualToNumber(2) {
            cell.cardName?.textColor = UIColor.blueColor()
        } else {
            if listed.cardClass.isEqualToNumber(3) {
                cell.cardName?.textColor = UIColor(red: 0xE2/255, green: 0xA8/255, blue: 0x79/255, alpha: 1.0)
            } else {
                if listed.cardClass.isEqualToNumber(4) {
                    cell.cardName?.textColor = UIColor(red: 0xFF/255, green: 0xAA/255, blue: 0x00/255, alpha: 1.0)
                } else {
                    if listed.cardClass.isEqualToNumber(5) {
                        cell.cardName?.textColor = UIColor(red: 0x22/255, green: 0x63/255, blue: 0x29/255, alpha: 1.0)
                    } else {
                        if listed.cardClass.isEqualToNumber(6) {
                            cell.cardName?.textColor = UIColor.brownColor()
                        } else {
                            if listed.cardClass.isEqualToNumber(7) {
                                cell.cardName?.textColor = UIColor(red: 0xBB/255, green: 0x76/255, blue: 0xE4/255, alpha: 1.0)
                            } else {
                                if listed.cardClass.isEqualToNumber(8) {
                                    cell.cardName?.textColor = UIColor(red: 0x9E/255, green: 0xB5/255, blue: 0xFF/255, alpha: 1.0)
                                } else {
                                    if listed.cardClass.isEqualToNumber(9) {
                                        cell.cardName?.textColor = UIColor.grayColor()
                                    } else {
                                        if listed.cardClass.isEqualToNumber(10) {
                                            cell.cardName?.textColor = UIColor.blackColor()
                                        }
                                        }
                                }
                            }

                            }
                        }
                    }
                }
        }
    }
    return cell
}

作为创建索引路径的方法,该索引路径被引用以定义实际被选中的单元格,但这并不完全正确。在线核心数据对象的在线覆盖很少。它在非核心数据时正常运行,但在核心数据时不正常。任何有关这方面的见解以及为什么我需要这样做都会受到赞赏,因为我仍在学习编程的一般细节!

1 个答案:

答案 0 :(得分:0)

我认为这是细胞重用的一个问题 - 滚动屏幕的细胞被放置在一个队列中,并从队列中拉出来在屏幕上滚动的行。在单元格滚动到屏幕之前对单元格所做的任何更改都会(默认情况下)不会重置,因此当您为不同的行将它们出列时,这些更改仍会保留。

您需要确保cellForRowAtIndexPath代码进行相关更改。复制代码的关键位:

var myDeckCards: DeckCards?
var cardCount = 0
let listed = frc.objectAtIndexPath(indexPath) as! Cards
for eachDeckCard in listed.cardselections {
    let myDeckCard = eachDeckCard as! DeckCards
    if myDeckCard.cardsstored == passedDeckObject {
        myDeckCards = eachDeckCard as? DeckCards
        cardCount = myDeckCards!.numberSelected.integerValue
    }
}
if cardCount == 0 {
    cell.cardCounter.text = "  "
    } else {
        if cardCount == 1 {
            cell.cardCounter.text = "  1"
        } else {
            if cardCount == 2 {
                cell.cardCounter.text = "  2"
        }
    }
}

didSelectRowAtIndexPathcellForRowAtIndexPath

didSelectRowAtIndexPath中,删除indexPathUpdate var,然后使用indexPath

另外,请注意那些嵌套的if语句是switch / case的主要候选者。