-1

我正在尝试显示一个节标题,其中包含单元格创建为节标题中的文本的月份和年份。这是我的代码,但它只显示一个部分标题。知道为什么以及如何让它显示单元格创建的年份和月份吗?

  import UIKit


class PRViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tblTasks : UITableView!

    //For persisting data
    let defaults = NSUserDefaults.standardUserDefaults()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tblTasks.reloadData()
         tblTasks.registerNib(UINib(nibName: "PRTableViewCell", bundle: nil), forCellReuseIdentifier: "PRTableCell")
        tblTasks.tableFooterView = UIView()

    }

    override func viewWillAppear(animated: Bool) {
        self.tblTasks.reloadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


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



        return 1

    }


    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "Your PR's"
    }

    //Define how our cells look - 2 lines a heading and a subtitle
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let identifier = "PRTableCell"
        var cell: PRTableViewCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? PRTableViewCell

        if cell == nil {
            tableView.registerNib(UINib(nibName: "PRTableViewCell", bundle: nil), forCellReuseIdentifier: identifier)
            cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? PRTableViewCell
        }


//        Assign the contents of our var "items" to the textLabel of each cell
//        cell.textLabel!.text = taskMgr.tasks[indexPath.row].name
//        cell.detailTextLabel!.text = taskMgr.tasks[indexPath.row].desc

        cell.PRLabel.text = taskMgr.tasks[indexPath.row].name
        cell.NotesLabel.text = taskMgr.tasks[indexPath.row].desc
        cell.WeightLabel.text = taskMgr.tasks[indexPath.row].weight + "lb"






        return cell



    }

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){

        if (editingStyle == UITableViewCellEditingStyle.Delete){

            taskMgr.removeTask(indexPath.row)
            tblTasks.reloadData()
        }


         func numberOfSectionsInTableView(tableView: UITableView) -> Int {

            // #warning Incomplete implementation, return the number of sections

            let numberOfSections = taskMgr.tasks.count



            return numberOfSections
        }





    }
4

2 回答 2

0

所以示例代码如下(代码未明显测试)。我假设在您的数组中保存的对象中有一个dateCreated包含 an 的属性。NSDatetasks

示例代码:

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

    return 1
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    let formatter = NSDateFormatter() // This is slightly inefficient for a large number of rows because setting up NSDateFormatter is expensive. You could make this a property. 
    formatter.dateStyle = .ShortStyle
    formatter.timeStyle = .NoStyle

    let sectionHeaderDate = taskMgr.tasks[section].dateCreated

    let dateString = formatter.stringFromDate(sectionHeaderDate)
    return dateString
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let identifier = "PRTableCell"
    var cell: PRTableViewCell! = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath:idx) as? PRTableViewCell // You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method (http://stackoverflow.com/q/12737860/558933).

    cell.PRLabel.text = taskMgr.tasks[indexPath.row].name
    cell.NotesLabel.text = taskMgr.tasks[indexPath.row].desc
    cell.WeightLabel.text = taskMgr.tasks[indexPath.row].weight + "lb" // Note iOS 9 allows you to localise weights rather than hard-coding "lb" or "kg". You should look at the documentation.

    return cell
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    let numberOfSections = taskMgr.tasks.count
    return numberOfSections
}

要向其中添加新的“部分”,UITableView您必须将新数据添加到数组中taskMgr.tasks,然后重新加载表或仅更新添加的行。将这些代码行包​​装在tblTasks.beginUpdates和中tblTasks.endUpdates。同样用于删除。

于 2016-05-15T21:35:01.923 回答
0

这是一种方法。请注意,此代码假定您的单元格数据位于名为“cellArray”的数组中。它显示的日期从今天开始,每个部分回溯一天。显然你需要替换你的日期。

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.cellArray.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath)

    // TODO: configure cell for display using self.cellArray[indexPath.section]

    return cell
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let df = NSDateFormatter()
    df.dateStyle = .MediumStyle
    df.timeStyle = .NoStyle

    // TODO: determine the actual date
    let displayDate = NSDate().dateByAddingTimeInterval(Double(section * -86400))

    return df.stringFromDate(displayDate)
}
于 2016-05-15T19:39:10.060 回答