Tyron
Tyron

Reputation: 88

2d array into table view swift

I am trying to populate a table view with a two dimensional array (struct). I have a class "Tag" which is made up of two strings and stored in core data, and a "TagsManager" which manages the tags. The first part of the array will be a single string as a group name which will divide the sections of the table view, and the second part will be a Tag, which I will only need to use one of that Tag's strings. I am stuck relating each tag to its group. If anyone could offer me a way of allowing the tags to be allocated to a group or can see a better solution to my problem it would be greatly appreciated!! Thank you.

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
    }

Upvotes: 0

Views: 2655

Answers (1)

Tyron
Tyron

Reputation: 88

For anyone wanting an answer to this question I have found a solution.

 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
    }

I hope this helps anyone trying to accomplish 2d arrays in tableViewCells!

Upvotes: 3

Related Questions