gpt4 book ai didi

swift - 核心数据 didselectatrow 更新文本字段

转载 作者:行者123 更新时间:2023-11-30 14:05:43 25 4
gpt4 key购买 nike

我有一个表格 View ,它向用户呈现自定义单元格,用户的选择会根据他们的选择更改 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 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
}

作为创建索引路径的一种方法,该索引路径引用来定义实际选择的单元格,但这并不完全正确。网上很少有关于针对核心数据对象执行此操作的内容。当不是核心数据时,它可以正常运行,但在使用核心数据时,它不能正常运行。任何对此的见解以及为什么我需要这样做将不胜感激,因为我仍在学习一般编程的来龙去脉!

最佳答案

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

您需要确保您的 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 的主要候选者。

关于swift - 核心数据 didselectatrow 更新文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32439646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com