二维数组进入 table 视图 swift
2d array into table view swift
我正在尝试使用二维数组(结构)填充 table 视图。我有一个 class "Tag" ,它由两个字符串组成并存储在核心数据中,还有一个 "TagsManager" 管理标签。数组的第一部分将是一个字符串作为组名,它将划分 table 视图的各个部分,第二部分将是一个标签,我只需要使用该标签的字符串之一.我坚持将每个标签与其组相关联。如果有人可以为我提供一种允许将标签分配给一个组的方法,或者可以找到更好的解决我的问题的方法,我将不胜感激!!谢谢。
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 中完成二维数组的人有所帮助!
我正在尝试使用二维数组(结构)填充 table 视图。我有一个 class "Tag" ,它由两个字符串组成并存储在核心数据中,还有一个 "TagsManager" 管理标签。数组的第一部分将是一个字符串作为组名,它将划分 table 视图的各个部分,第二部分将是一个标签,我只需要使用该标签的字符串之一.我坚持将每个标签与其组相关联。如果有人可以为我提供一种允许将标签分配给一个组的方法,或者可以找到更好的解决我的问题的方法,我将不胜感激!!谢谢。
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 中完成二维数组的人有所帮助!