2d数组进入表视图swift

时间:2015-06-22 21:54:11

标签: arrays swift struct tableview

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

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
    }

1 个答案:

答案 0 :(得分:2)

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

@using (Html.BeginForm("GetPurchasedItem","YourController")) {      

 @Html.DropDownList("SupplierId", ViewBag.PurSupName as SelectList, new  {@class = "form-control",style = "width: 200px;",id="supId"})   
  <input type="submit" value="OK" />      
}

我希望这可以帮助任何人尝试在tableViewCells中完成2d数组!