gpt4 book ai didi

swift - didselectrow 更新下面的多个单元格重新加载 TableView + 计数器

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

这是一个由两部分组成的问题,第一个问题是在我花了整整一周试图弄清楚这一点之后,我仍然一片空白。我终于让各个单元格显示与文本字段中的选择相关的适当数字,但仍然没有弄清楚为什么重新加载到表格 View 中的单元格也显示更新的文本字段。基本上,我只想仅在选定的行中更新 cardCounter 的文本字段,然后如果用户滚动回原来的位置(如果他们决定删除它),则会向他们显示该对象中有多少可用的计数器。

第二个问题是我正在尝试为总限制为 30 的 cardCount 创建一个计数器,并且对于所选的每个 cardCount 对象,这 30 个选择中的 1 个已缩小到 29、28、27 等。只是想完成我的第一个项目的一些小代码。

import UIKit
import CoreData

class CardSelectionViewController: UIViewController, UITableViewDelegate, UIAlertViewDelegate, UISearchBarDelegate, NSFetchedResultsControllerDelegate {

var passedDeckObject: Deck?
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

var deckCardCount = 30

@IBOutlet weak var cardsListed: UITableView!

@IBOutlet weak var cardSearch: UISearchBar!


@IBAction func backButton(sender: AnyObject) {
}

@IBAction func saveCardSelection(sender: AnyObject) {

if deckCardCount != 30 {

let selectmorecards = UIAlertController(title: "Select More Cards", message: "Please Select More Cards To Complete Deck", preferredStyle: .Alert)
let okCardAction: UIAlertAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
selectmorecards.addAction(okCardAction)
self.presentViewController(selectmorecards, animated: true, completion: nil)

} else {

if deckCardCount == 30 {
let alertController = UIAlertController(title: "All Cards Correct?", message: "Are all the cards in the deck what you wanted?", preferredStyle: .Alert)
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
let okAction: UIAlertAction = UIAlertAction(title: "OK", style: .Default) { (action) -> Void in
self.managedObjectContext?.save(nil)

self.presentViewController(alertController, animated: true, completion: nil)

}
}
}
}

var costfrc: NSFetchedResultsController!
var frc: NSFetchedResultsController = NSFetchedResultsController()
func getFetchedResultsController() -> NSFetchedResultsController {
frc = NSFetchedResultsController(fetchRequest: listFetchRequest(), managedObjectContext: managedObjectContext!, sectionNameKeyPath: nil, cacheName: nil)

return frc

}
func listFetchRequest() -> NSFetchRequest {

let requiredClassName = passedDeckObject!.classSelected.classname
var requiredCardClasses = [Int]()
let fetchRequest = NSFetchRequest(entityName: "Cards")
let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
switch(requiredClassName) {
case "Warrior":
requiredCardClasses = [1,10]
case "Shaman":
requiredCardClasses = [2,10]
case "Rogue":
requiredCardClasses = [3,10]
case "Paladin":
requiredCardClasses = [4,10]
case "Hunter":
requiredCardClasses = [5,10]
case "Druid":
requiredCardClasses = [6,10]
case "Warlock":
requiredCardClasses = [7,10]
case "Mage":
requiredCardClasses = [8,10]
case "Priest":
requiredCardClasses = [9,10]
default:
requiredCardClasses = [10]
}
fetchRequest.sortDescriptors = [sortDescriptor]
fetchRequest.predicate = NSPredicate(format:"cardClass IN %@", requiredCardClasses)

return fetchRequest
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let numberofRowsInSection = frc.sections?[section].numberOfObjects
return numberofRowsInSection!
}


//Mark cell displayed with colored text//
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
var cardCount = 0
var myDeckCards: DeckCards?
for eachDeckCard in listed.cardselections {
let myDeckCard = eachDeckCard as! DeckCards
if myDeckCard.cardsstored == passedDeckObject {
myDeckCards = eachDeckCard as? DeckCards
cardCount = myDeckCards!.numberSelected.integerValue
}
}




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
}
//reload content//
func controllerDidChangeContent(controller: NSFetchedResultsController) {
cardsListed.reloadData()
}


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


var cell = cardsListed.cellForRowAtIndexPath(indexPath) as! firstCardDetails

cell.indexOfAccessibilityElement(listed)

// 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
myDeckCards!.numberSelected = cardCount
deckCardCount = deckCardCount - cardCount
cell.cardCounter.text = " " + cardCount.description
if deckCardCount == 30 {
if cell.selected {
deckCardCount = deckCardCount + 1
}
myDeckCards!.numberSelected = deckCardCount
myDeckCards!.numberSelected = cardCount
}
println(deckCardCount)
println(cardCount)

}

//searchBar contoller//
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {

searchBar.setShowsCancelButton(true, animated: true)
let requiredClassName = passedDeckObject!.classSelected.classname
var requiredCardClasses = [Int]()
switch(requiredClassName) {
case "Warrior":
requiredCardClasses = [1,10]
case "Shaman":
requiredCardClasses = [2,10]
case "Rogue":
requiredCardClasses = [3,10]
case "Paladin":
requiredCardClasses = [4,10]
case "Hunter":
requiredCardClasses = [5,10]
case "Druid":
requiredCardClasses = [6,10]
case "Warlock":
requiredCardClasses = [7,10]
case "Mage":
requiredCardClasses = [8,10]
case "Priest":
requiredCardClasses = [9,10]
default:
requiredCardClasses = [10]
}
if !searchText.isEmpty {
// Clear out the fetchedResultController
frc = getFetchedResultsController()
// Setup the fetch request
let fetchRequest = NSFetchRequest(entityName: "Cards")
fetchRequest.fetchLimit = 565
fetchRequest.predicate = NSCompoundPredicate(type: .AndPredicateType, subpredicates: [NSPredicate(format: "name contains[cd] %@", searchText), NSPredicate(format: "cardClass IN %@", requiredCardClasses)])
let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
println(fetchRequest.predicate)
//end//
//cost fetch request begins here//
let costfetchRequest = NSFetchRequest(entityName: "cardCost")
costfetchRequest.fetchLimit = 565
costfetchRequest.predicate = NSPredicate(format: "cardCost = %i", searchText)
let costSortDescriptor = NSSortDescriptor(key: "cardCost", ascending: true)
costfetchRequest.sortDescriptors = [sortDescriptor]
//end//

// Pass the fetchRequest and the context as parameters to the fetchedResultController
frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext:managedObjectContext!,
sectionNameKeyPath: nil, cacheName: nil)
// Make the fetchedResultController a delegate of the MoviesViewController class
frc.delegate = self
// Execute the fetch request or display an error message in the Debugger console
var error: NSError? = nil
if (!frc.performFetch(&error)) {
println("Error: \(error?.localizedDescription)")
}
// Refresh the table view to show search results
cardsListed.reloadData()
}
}
//search Cancel
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchBar.text = nil
searchBar.showsCancelButton = false // Hide the cancel
searchBar.resignFirstResponder() // Hide the keyboard

// Refresh the table view to show fetchedResultController results
cardsListed.reloadData()
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

self.view.endEditing(true)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {

textField.resignFirstResponder()
return true
}

override func viewDidLoad() {
super.viewDidLoad()

println(passedDeckObject?.deckname)
var nib = UINib(nibName: "firstCardDetails", bundle: nil)
cardsListed.registerNib(nib, forCellReuseIdentifier: "cell")
frc = getFetchedResultsController()
frc.delegate = self
frc.performFetch(nil)


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

最佳答案

对于问题 #1...

当您使用可重用单元格时,在 cellForRowAtIndexPath(或 prepareForReuse)之外进行单元格更改通常不是一个好主意。也有异常(exception),但大多数时候,这是安全的选择。最好的策略是在其他方法中进行数据更改,然后重新加载受影响的单元格(或全部)。

由于单元格已缓存,因此每当使用单元格时,您对单元格所做的更改都会显示在屏幕上,除非您在返回单元格时明确覆盖更改。

您应该计算所需的内容并将其存储在 didSelectRowAtIndexPath 方法中的某个位置,然后在 cellForRowAtIndexPath 中更新它的显示。

关于swift - didselectrow 更新下面的多个单元格重新加载 TableView + 计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32608336/

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