2017-08-28 87 views
0

我想在表格视图中添加部分。但我有一个致命的错误。我的代码有什么问题?任何更好的方式来做到这一点?如何在分组表格视图中添加部分?

这是我的参考: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

回答

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 
}