如何在分组表视图中添加部分?

时间:2017-08-28 09:46:37

标签: swift uitableview

我正在尝试在表格视图中添加部分。但是我得到了一个致命的错误。我的代码有什么问题?有没有更好的方法呢?

这是我的参考:https://github.com/KoheiHayakawa/DateCell 但参考文献没有章节。如何以编程方式添加部分?

let kDateCell = "dateCell1"
let kDatePickerCell = "datePickerCell1"
let kOtherCell = "otherCell1"

var dateArray : [[String: AnyObject]] = []
var dateArray2 : [[String: AnyObject]] = []

viewDidLoad中

    //Section 1
    let itemOne = [titleKey: "Start Time", dateKey: nextFirst] as [String : Any]
    let itemTwo = [titleKey: "End Time", dateKey: nextFirst] as [String : Any]
    let itemThree = [titleKey: "Apply to All"]
    self.dateArray = [itemOne as Dictionary<String, AnyObject>, itemTwo as Dictionary<String, AnyObject>, itemThree as Dictionary<String, AnyObject>]

    //Section 2
    let dayOne = [titleKey: "31/8"]
    let dayTwo = [titleKey: "1/9"]
    let dayThree = [titleKey: "2/9"]
    let dayFour = [titleKey: "3/9"]
    let dayFive = [titleKey: "4/9"]
    let daySive = [titleKey: "5/9"]
    let daySeven = [titleKey: "6/9"]

    self.dateArray2 = [dayOne as Dictionary<String, AnyObject>, dayTwo as Dictionary<String, AnyObject>, dayThree as Dictionary<String, AnyObject>,dayFour as Dictionary<String, AnyObject>, dayFive as Dictionary<String, AnyObject>, daySive as Dictionary<String, AnyObject>, daySeven as Dictionary<String, AnyObject>]

func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if (self.hasInlineDatePicker()) {
        return dateArray.count + 1;
    }else if(section == 1){
        return dateArray2.count
    }
    return 9

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell: UITableViewCell?
    var cellID = kOtherCell

    if (self.indexPathHasPicker(indexPath)) {
        cellID = kDatePickerCell;
    } else if (self.indexPathHasDate(indexPath)) {
        cellID = kDateCell;
    }

    cell = tableView.dequeueReusableCell(withIdentifier: cellID)

    var modelRow = indexPath.row
    if (self.datePickerIndexPath != nil && self.datePickerIndexPath!.row <= indexPath.row) {
        modelRow -= 1
    }
    //fatal error: Index out of range here.
    let itemData = self.dateArray[modelRow]

    if (cellID == kDateCell) {
        cell!.textLabel!.text = itemData[titleKey] as? String
        cell!.detailTextLabel!.text = self.dateFormatter.string(from: itemData[dateKey] as! Date)
    } else if (cellID == kOtherCell) {
        cell!.textLabel!.text = itemData[titleKey] as? String
    }

    return cell!

}

这是应用布局。 Layout

1 个答案:

答案 0 :(得分:0)

要以编程方式添加节,您需要使用以下方法之一:

@available(iOS 2.0, *)
optional public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?

@available(iOS 2.0, *)
optional public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

第一个将添加一个带有UILabel

的默认标题部分
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section == 0 {
        return "Next week\nDate...."
    } else {
        return "Specific date and time"
    }
}

第二个将为您提供更多选项,因为您可以注入自己的自定义UIView

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let customHeaderView = MyCustomHeaderView()

    // configure custom view

    return customHeaderView
}

编辑:如果您拥有多个部分,则还需要实施以下方法(已有):

@available(iOS 2.0, *)
optional public func numberOfSections(in tableView: UITableView) -> Int

例如:

func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}