当委托和数据源类是分开的时,如何处理tableview单元格单击和segues?

时间:2017-10-03 09:44:00

标签: ios swift uitableview

我试图将TableViewDelegate和TableViewDataSource分离到ViewController中的一个单独的类,现在我遇到了一些问题。

第一个问题:

App会加载包含所有内容的tableview,但是当我点击它或尝试滚动时,所有数据都会消失。

第二个问题: 单击单元格应链接到另一个显示更多内容的视图。我使用函数将数据推送到此视图。但是当我将委托和数据源分离到其他类时,它不起作用。

prepare(for segue: UIStoryboardSegue, sender: Any?)

以下是我的视图控制器代码:

import UIKit
import Foundation
import os


class FirstViewController: UIViewController {


@IBOutlet weak var tableview: UITableView!
@IBOutlet weak var offlineModePicture: UIBarButtonItem!

@IBOutlet weak var refresh_button: UIBarButtonItem!


var wyznania_page = 0 // page
var isNewDataLoading = false

var wyznania = [[WyznanieData](),[WyznanieData](),[WyznanieData](),[WyznanieData](),[WyznanieData]()]




let activitiyViewController = ActivityViewController(message: "Ładowanie...")




override func viewDidLoad() {
    super.viewDidLoad()

    wyznania[wyznania_page].append(WyznanieData(date: "date", story: "story", sharedLink: "link", tag: "asd", fav: false, page: 1)!)

    wyznania[wyznania_page].append(WyznanieData(date: "date", story: "story", sharedLink: "link", tag: "asd", fav: false, page: 1)!)
    wyznania[wyznania_page].append(WyznanieData(date: "date", story: "story", sharedLink: "link", tag: "asd", fav: false, page: 1)!)





    self.navigationController?.navigationBar.sizeToFit()
    view.backgroundColor = UIColor.black


    tabBarController?.tabBar.barTintColor = ColorsUI.bar
    tabBarController?.tabBar.tintColor = UIColor.white
    navigationController?.navigationBar.barTintColor = ColorsUI.bar
    navigationController?.navigationBar.tintColor = UIColor.white

    let customDelegate = TableViewDelegate(dataForRows: wyznania[wyznania_page])

    self.tableview.delegate = customDelegate
    self.tableview.dataSource = customDelegate




}

override internal var preferredStatusBarStyle : UIStatusBarStyle {
    return .lightContent
}




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


    super.prepare(for: segue, sender: sender)

    switch(segue.identifier ?? "") {


    case "ShowDetail":
        guard let storyDetailViewController = segue.destination as? WyznanieViewController else {
            fatalError("Unexpected destination: \(segue.destination)")
        }

        guard let selectedStopCell = sender as? Wyznanie else {
            fatalError("Unexpected sender: \(String(describing: sender))")
        }

        guard let indexPath = tableview.indexPath(for: selectedStopCell) else {
            fatalError("The selected cell is not being displayed by the table")
        }



        let selectedStory = wyznania[wyznania_page][(indexPath as NSIndexPath).row]
        storyDetailViewController.wyznanie = selectedStory




    default:
        fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))")
    }


}












@IBAction func unwindToList(sender: UIStoryboardSegue) {
    if let sourceViewController = sender.source as? WyznanieViewController, let story = sourceViewController.wyznanie {

        if let selectedIndexPath = tableview.indexPathForSelectedRow {
            // Update an existing story.
            print("updating")
            wyznania[wyznania_page][selectedIndexPath.row] = story
            tableview.reloadRows(at: [selectedIndexPath], with: .none)
        }
        else {
            // Add a new story
            print("adding new")


        }


    }
}

}

我的Delegate和DataSource类:

[import UIKit

class TableViewDelegate: NSObject,UITableViewDelegate,UITableViewDataSource {


    var wyznania = \[WyznanieData\]()



    init(dataForRows: \[WyznanieData\]) {
        self.wyznania = dataForRows
        super.init()
    }


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

    func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
    }


    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1

    }

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

        return wyznania.count

    }



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


        guard let cell = tableView.dequeueReusableCell(withIdentifier: "story_cell", for:indexPath) as? Wyznanie  else {
            fatalError("The dequeued cell is not an instance of WyznanieTableViewCell.")
        }


        let wyznanie = wyznania\[indexPath.row\]

        cell.date.text = wyznanie.date
        cell.story.text = wyznanie.story
        cell.story.setContentOffset(CGPoint.zero, animated: true)
        cell.story.textColor = UIColor.white
        cell.backgroundColor = ColorsUI.cell_backgroung
        cell.layer.borderWidth = 3.0
        cell.layer.borderColor = ColorsUI.cell_borderColor
        return cell

    }

}]

1

[demo]

1 个答案:

答案 0 :(得分:1)

尝试将delegate变量设为全局变量。当viewDidLoad中的范围超出范围时必须重新分配(dataSourcedelegate在UITableView中很弱)。

提取以下声明全局。

var customDelegate: TableViewDelegate!

然后在viewDidLoad中执行以下操作

customDelegate = TableViewDelegate(dataForRows: wyznania[wyznania_page])