简体   繁体   中英

How to design more views sharing a tableview using the storyboard

I would like to have multiple views, each containing 2 tableviews (taking half screen each). One of the tableviews will be shared by each view. The tableviews will behave more or less independently.

I thought of two options:

1) Independent views in which the shared tableview is reloaded whenever the view comes into place.

2) One single tableview shared by each view (similar to what happens in the split view controller (though I cannot use the splitview as I'd like to use adaptive layout when the devices are in portrait mode).

I have the feeling that the second approach would be best, but I could not think of a way to design it. What is the best way to obtain this?

I have tried to do as suggested by Andriy Gordiychuk (answer 1), but I think I am messing things up....:
When I run the app, it builds successfully, but it will crash whenever I try to get to the shared view with an EXC_BAD_ACCESS error.

Here is the SharedReusableTableViewDelegate:

class SharedReusableTableViewDelegate: UIViewController, UITableViewDelegate, UITableViewDataSource {

var databasePath = String()
var selectedVerse : Int = 1

func registerClass(UITableViewCell, forCellReuseIdentifier identifier: String){}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let row : Int = indexPath.row
    var getVerse = DatabaseDB()
    var verse = DatabaseVars()
    (verse.book, verse.chapter, verse.verseNumber, verse.extra, verse.verseText) = getVerse.getVerseBy(row, database: databasePath as String)
    let cell: AnyObject = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
    cell.textLabel?!.text = "\(verse.chapter),\(verse.verseNumber) \(verse.verseText)"
    return cell as! UITableViewCell
}

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let numberofCells = 200
    return numberofCells
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        selectedVerse = indexPath.row + 1
        println("You selected cell #\(selectedVerse)!")
}

}

And here is the controller for the view with Table1:

class Table1: UIViewController {

@IBOutlet weak var verseView: UITableView!
var databasePath = NSString()
var currentPath = NSString()

override func viewDidLoad() {
    super.viewDidLoad()

    let filemgr = NSFileManager.defaultManager()
    let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let docsDir = dirPaths[0] as! String
    databasePath = docsDir.stringByAppendingPathComponent("LuceDaLuce.db")
    var fileCopyError : NSError? = NSError()
    currentPath = NSBundle.mainBundle().pathForResource("tmp", ofType: "db")!
    println("currentPath: \(currentPath)")
    if !filemgr.fileExistsAtPath(databasePath as String) {
        if filemgr.copyItemAtPath(currentPath as String, toPath: databasePath as String, error: &fileCopyError) {
            println("Copy successful")
        } else {
            println("Copy failed with error: \(fileCopyError!.localizedDescription)")
        }
    } else {println("The database is already there")}

    let delegate = SharedReusableTableViewDelegate()
    delegate.databasePath = databasePath as String
    verseView.delegate = delegate
    verseView.dataSource = delegate
}
}

I am loading the content of the cells from a sqlite database (which I have already tested).

Any advice?
Thanks,
S

Create a class which will be the data source/delegate for your shared table views:

class SharedReusableTableViewDelegate:UITableViewDelegate,UITableViewDataSource {

    //put below all the delegate/data source functions

}

All you have to do now is to create IBOutlet s to your tables in the view controllers in which you have them and then in viewDidLoad add the following:

let delegate = SharedReusableTableViewDelegate()
mySharedTableView.delegate = delegate
mySharedTableView.dataSource = delegate

and that's it.

With this setup you are separating the logic which fills the tables from the logic which draws them. You have to create your UI design for each view controller scene and then you simply have to assign to each reused table view the correct delegate. And that delegate contains all of the code in one single file.

I thought that this is what you wanted to achieve. If not, can you please clarify what exactly you would like to achieve?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM