gpt4 book ai didi

arrays - 二维数组快速进入表格 View

转载 作者:行者123 更新时间:2023-11-28 13:11:49 27 4
gpt4 key购买 nike

我正在尝试使用二维数组(结构)填充 TableView 。我有一个由两个字符串组成并存储在核心数据中的“Tag”类,以及一个管理标签的“TagsManager”。数组的第一部分将是一个字符串作为组名,它将划分 TableView 的各个部分,第二部分将是一个标签,我只需要使用该标签的字符串之一。我坚持将每个标签与其组相关联。如果有人可以为我提供一种允许将标签分配给一个组的方法,或者可以找到更好的解决我的问题的方法,我将不胜感激!!谢谢。

class TagsManager {

struct Group {

var name: String!
var tags: [Tag]
}
//Sone tagsManager functions here


func findByGroup(tagLabel: String) -> [Tag] {
var fetchRequest = NSFetchRequest(entityName: Tag.EntityName)
fetchRequest.predicate = NSPredicate(format: "tagName = %@", tagLabel)

return managedObjectContext.executeFetchRequest(fetchRequest, error: nil) as? [Tag] ?? [Tag]()
}


func findAllByGroup() -> [Group] {

var unsortedFindAll = findAll()
var groupNames = Set<String>()

for tag in unsortedFindAll {
groupNames.insert(tag.tagName)

}
var results = [Group]()
for groupName in groupNames {
let groupTags = findByGroup(groupName)
let group = Group(name: groupName, tags: groupTags)
results.append(group)
}
return results
}

func findAll() -> [Tag] {
let fetchRequest = NSFetchRequest(entityName: Tag.EntityName)
let sortDescriptor = NSSortDescriptor(key: "tagName", ascending: true)

fetchRequest.sortDescriptors = [sortDescriptor]

// TODO: Check what happens if the array is empty
return managedObjectContext.executeFetchRequest(fetchRequest, error: nil) as? [Tag] ?? [Tag]()
}


}

class IndividualAnimalTableViewController: UITableViewController, UITextFieldDelegate, UITextViewDelegate, UIAlertViewDelegate, SDLStickReaderListener, StickReaderDelegate, UITabBarControllerDelegate {

var groupsKnown = [Group]()



override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
return self.groupsKnown[section].name
}


func fetchKnownTags() {
groupsKnown = knownTagManager.findAllByGroup()
tagsKnown = knownTagManager.findAll()
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ??

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections
return groupsKnown.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("textInputCell", forIndexPath:indexPath) as! TagCell
if self.tagsKnown.count != 0 {
fetchKnownTags()

let tag = self.tagsKnown[indexPath.row]
let group = self.groupsKnown[indexPath.section]
//return other cells
self.deleteButton.hidden = false
cell.TagNumberTxt.font = StoryBoard.myFont
cell.TagNumberTxt.textColor = StoryBoard.cellColour
//Not sure how to handle this part//
cell.TagNumberTxt.text = ""
}
return cell
}

最佳答案

对于任何想要回答这个问题的人,我已经找到了解决方案。

 override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
return sortedGroups[section].name
}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.groupsKnown[section].tags.count
}

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

let cell = tableView.dequeueReusableCellWithIdentifier("textInputCell", forIndexPath:indexPath) as! TagCell
if self.tagsKnown.count != 0 {
fetchKnownTags()
var indexPathRow = (indexPath.row)
let group = groupsKnown[indexPath.section].tags[indexPathRow]
//return other cells
self.deleteButton.hidden = false
cell.TagNumberTxt.font = StoryBoard.myFont
cell.TagNumberTxt.textColor = StoryBoard.cellColour
cell.TagNumberTxt.text = "\(group.tagNumber)"
}
return cell
}

我希望这可以帮助任何试图在 tableViewCells 中完成二维数组的人!

关于arrays - 二维数组快速进入表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30990492/

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