如何在TableViewCell(swift)中插入第一个新行?

时间:2017-10-25 14:43:00

标签: ios swift uitableview

人们告诉我如何摆脱我的立场并这样做,以便在添加新事件时它成为开始,而不是结束。我将非常感谢你的帮助。

我有三个数组,我不得不通过父单元格对它们进行排序,在这一行中我非常喜欢func tableView(_ tableView:UITableView,numberOfRowsInSection section:Int) - > INT

import UIKit
import Parse


class PrognozSegmentViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {


        var soccerString = [Soccer]()
        var basketString = [Basketball]()
        var tennisString = [Tennis]()


    var refresh : UIRefreshControl!




    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var segmentControl: UISegmentedControl!
    @IBAction func btnSegment(_ sender: Any) {

        tableView.reloadData()

    }




    override func viewDidLoad() {
        super.viewDidLoad()



        refresh = UIRefreshControl()
        refresh.attributedTitle = NSAttributedString(string: "Потяните вниз чтоб обновить ресурс")
        refresh.tintColor = UIColor.green
        refresh.addTarget(self, action: #selector(PrognozSegmentViewController.refreshing), for: UIControlEvents.valueChanged)
        tableView.addSubview(refresh)
        setupSeg()
        tableView.tableFooterView = UIView()
        tableView.estimatedRowHeight = 88
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        tableView.register(UINib(nibName:"TableViewCell",bundle:nil), forCellReuseIdentifier: "sportCell")


        loadObjects1()
        loadObjects2()
        loadObjects3()

    }




    func setupSeg() {

        let attributes = [ NSAttributedStringKey.foregroundColor : UIColor.yellow,
                           NSAttributedStringKey.font : UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.bold)];
        let attributesSelected = [ NSAttributedStringKey.foregroundColor : UIColor.black,
                                   NSAttributedStringKey.font : UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.bold)];

        segmentControl.setTitleTextAttributes(attributes, for: UIControlState.normal)
        segmentControl.setTitleTextAttributes(attributesSelected, for: UIControlState.selected)

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var returnSport = 0
        switch (segmentControl.selectedSegmentIndex) {
        case 0 :
             returnSport = soccerString.count
            break
        case 1 :
             returnSport = basketString.count
            break
        case 2 :
               returnSport = tennisString.count
            break
        default :
            break
        }
        return returnSport
    }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let sportCell = tableView.dequeueReusableCell(withIdentifier: "sportCell", for: indexPath) as! TableViewCell
                        switch (segmentControl.selectedSegmentIndex)  {
            case 0 :



                let soccer = soccerString[indexPath.row]

                sportCell.matchLabel.text = soccer.matchS
                            soccerString[indexPath.row].imagePrS.getDataInBackground {(data, error) in
                    sportCell.imageMatch.image = error == nil ? UIImage(data: data!) : nil

                }
                break
            case 1 :

                let basket = basketString[indexPath.row]
                sportCell.matchLabel.text = basket.matchB
                basketString[indexPath.row].imagePrB.getDataInBackground {(data, error) in
                    sportCell.imageMatch.image = error == nil ? UIImage(data: data!) : nil

                }
                break
            case 2:
                sportCell.matchLabel.text = tennisString[indexPath.row].matchT

                tennisString[indexPath.row].imagePrT.getDataInBackground {(data, error) in
                        sportCell.imageMatch.image = error == nil ? UIImage(data: data!) : nil

                }
               break
            default:
                break

                }
            sportCell.selectionStyle = UITableViewCellSelectionStyle.none

            let date = Date()
            let calendar = Calendar.current

            let yesterday = calendar.date(byAdding: .day, value: -1, to: date)
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "EEEE, MMM:d, yyyy"
            dateFormatter.dateStyle = .short
            dateFormatter.timeStyle = .short
            dateFormatter.locale = Locale(identifier: "ru_UA")
            dateFormatter.doesRelativeDateFormatting = true

            let dataFormater2 = DateFormatter()
            dataFormater2.dateFormat = "EEEE, MMM:d, yyyy"
            dataFormater2.dateStyle = .short
            dataFormater2.timeStyle = .short
            dataFormater2.locale = Locale(identifier: "ru_UA")
            dataFormater2.doesRelativeDateFormatting = true


                sportCell.dataSave.text! = dateFormatter.string(from: date)
                sportCell.dataSave.text = dateFormatter.string(from: yesterday!)





                return sportCell
            }


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

     func loadObjects1() {
        let query = Soccer.query() as! PFQuery<Soccer>

        query.findObjectsInBackground  { (objects, error) in
            if error == nil {
                self.soccerString.removeAll()

                self.soccerString = objects!

                self.tableView.reloadData()
            } else {
                print(error!)
            }

            }
    }

        func loadObjects2() {
        let query = Basketball.query() as! PFQuery<Basketball>

        query.findObjectsInBackground  { (objects, error) in
            if error == nil {
                self.basketString.removeAll()

                self.basketString = objects!

                self.tableView.reloadData()
            } else {
                print(error!)
            }

        }
    }

    func loadObjects3() {
        let query = Tennis.query() as! PFQuery<Tennis>


        query.findObjectsInBackground  { (objects, error) in
            if error == nil {
                self.tennisString.removeAll()

                self.tennisString = objects!


                self.tableView.reloadData()
            } else {
                print(error!)
            }

        }
    }


    @objc func refreshing() {

        loadObjects1()
        loadObjects2()
        loadObjects3()
        tableView.reloadData()
        refresh.endRefreshing()

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {


        switch (segmentControl.selectedSegmentIndex) {


            case 0 :
                if segue.identifier == "showSoccer" {
                    if let indexPath = tableView.indexPathForSelectedRow {
            let destationViewController = segue.destination as! DetailSoccerTableViewController

            destationViewController.detailSoccer = self.soccerString[indexPath.row]


                    }
            }



            case 1 :
                if segue.identifier == "showBasket" {
                    let dvc = segue.destination as! DetailBasketViewController
                    if let indexPath = tableView.indexPathForSelectedRow {
                        let row = Int(indexPath.row)
                        dvc.baskets = basketString[row]
                    }

            }

             case 2 :
                let destationVC: DetailTennisViewController = segue.destination as! DetailTennisViewController
                if let indexPath = self.tableView.indexPathForSelectedRow {
                    let row = Int(indexPath.row)
                    destationVC.tenises = tennisString[row]

                }

                self.present(destationVC, animated: true, completion: nil)


            default :
                break



            }
        }



    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

             switch ( segmentControl.selectedSegmentIndex) {

             case 0 :
                let soccer = soccerString[indexPath.row]

              performSegue(withIdentifier: "showSoccer", sender: soccer)


                break
             case 1 :

                let basket = basketString[indexPath.row]
                let destinationVC = DetailBasketViewController()
                destinationVC.baskets = basket
                self.performSegue(withIdentifier: "showBasket", sender: self)
                break
             case 2 :

                let tennis = tennisString[indexPath.row]
                let destTenVC = DetailTennisViewController()
                destTenVC.tenises = tennis

                self.performSegue(withIdentifier: "showTennis", sender: self)

               break
             default :
                break


        }

    }



}

1 个答案:

答案 0 :(得分:1)

如果您的问题是新对象成为最后一个

我认为你正在谈论一个阵列并且你正在做

array.append(item)

如果你想让项目在开头做:

array.insert(item, at: 0)

然后

tableView.reloadData()