2014-10-06 18 views
6

Tôi có đoạn code sau để hiển thị hai bảng cư từ hai mảng khác nhau trong một quan điểm:Hai bảng trên một cái nhìn trong nhanh chóng

@IBOutlet var RFTable: UITableView 
    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 

    } 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.RFTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 
    } 
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { 
     return self.RFArray.count; 
    } 
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) ->  UITableViewCell! { 
     var cell:UITableViewCell = self.RFTable.dequeueReusableCellWithIdentifier("cell") as  UITableViewCell 

     cell.textLabel.text = String(self.RFArray[indexPath.row]) 

     return cell 
    } 

    @IBOutlet var IMProdTable: UITableView 
    func tableView2(IMProdTable: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)  { 

    } 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.IMProdTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell2") 
    } 
    func tableView2(IMProdTable: UITableView!, numberOfRowsInSection section: Int) -> Int { 
    return self.IMProdArray.count; 
    } 
    func tableView2(IMProdTable: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) ->  UITableViewCell! { 
     var cell2:UITableViewCell = self.IMProdTable.dequeueReusableCellWithIdentifier("cell2") as UITableViewCell 

     cell2.textLabel.text = String(self.IMProdArray[indexPath.row]) 

     return cell2 
    } 

tôi đã làm việc bảng đầu tiên, và sau đó sao chép và dán văn bản, thay thế tên mảng và tên bảng, và đã kết nối đại biểu và nguồn dữ liệu. Tuy nhiên Xcode hiển thị 'redeclaration không hợp lệ của viewdidload' trên mã thứ hai (được dán). Nếu tôi thay thế điều này thành 'loadView của quỹ() {' thay vì viewdidload ứng dụng được tạo. Khi tôi thử nghiệm nó mặc dù, cả hai bảng xem chính xác cùng một dữ liệu đó là dữ liệu trong 'RFArray.' Tôi rất mới để viết mã và không thể nhìn thấy những gì tôi đã làm, xin vui lòng giúp đỡ.

Trả lời

11
@IBOutlet var RFTable: UITableView 
@IBOutlet var IMProdTable: UITableView 

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 

} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.RFTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 
    self.IMProdTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell2") 
} 

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { 
    if tableView == RFTable { 
    return self.RFArray.count; 
    } else { 
    return self.IMProdArray.count; 
    } 
} 

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) ->  UITableViewCell! { 
    if tableView == RFTable { 
    var cell:UITableViewCell = self.RFTable.dequeueReusableCellWithIdentifier("cell") as  UITableViewCell 
    cell.textLabel.text = String(self.RFArray[indexPath.row]) 
    return cell 
    } else { 
    var cell2:UITableViewCell = self.IMProdTable.dequeueReusableCellWithIdentifier("cell2") as UITableViewCell 
    cell2.textLabel.text = String(self.IMProdArray[indexPath.row]) 
    return cell2 
    } 
} 

Chỉ cần chỉnh sửa nhanh. Bạn cần phải giữ cho các phương thức đại biểu và nguồn dữ liệu giống nhau và kiểm tra xem cá thể TableView nào thực sự đang gửi thư.

Bạn không thể ghi đè cùng một phương thức hai lần trong lớp dẫn xuất.

+1

Cảm ơn bạn rất nhiều, có ý nghĩa hoàn hảo, cảm ơn bạn đã giải thích. Làm việc lần đầu – samp17

0

Đồng thời đảm bảo tải lại từng TableView Sau khi tìm nạp dữ liệu vào TableviewCell.

ví dụ

@IBOutlet var RFTable: UITableView 
@IBOutlet var IMProdTable: UITableView 

override func viewDidLoad() { 

    super.viewDidLoad() 
    self.RFTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell1") 
    self.IMProdTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell2") 

    RFTable.reloadData() 
    IMProdTable.reloadData() 
} 
2

Đầu tiên tạo ra hai lớp DataSource thực hiện nguồn First Data

nguồn
class FirstDataSouce: NSObject,UITableViewDataSource,UITableViewDelegate { 

    var items: [String] = [] 


    override init(){ 
     super.init() 
    } 

    func setData(items:[String]){ 
     self.items = items 
    } 



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return items.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "RecentTableViewCell") as! RecentTableViewCell 

     cell.titleLabel.text = items[indexPath.row] 

    return cell 
    } 
} 

dữ liệu thứ hai

class SecondDataSouce: NSObject,UITableViewDataSource,UITableViewDelegate { 

    var items: [String] = [] 


    override init(){ 
     super.init() 
    } 

    func setData(items:[String]){ 
     self.items = items 
    } 



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return items.count 
    } 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "RecentTableViewCell") as! RecentTableViewCell 

     cell.titleLabel.text = items[indexPath.row] 

    return cell 
} 
} 

Set nguồn dữ liệu để tableview trong ViewController

012.351.
class ViewController: UIViewController{ 
    @IBOutlet weak var tableView1: UITableView! 
    @IBOutlet weak var tableView2: UITableView! 

    var dataSource1: FirstDataSouce! 
    var dataSource2: SecondDataSouce! 

    func prepareTableViews(){ 

     let items1 = [“a”,”b”,”c”] 
     dataSource1 = FirstDataSouce() 
     dataSource1.setData(items: items1) 
     self.tableView1.dataSource = dataSource1 
     self.tableView1.delegate = dataSource1 
     self.tableView1.register(SelectorTableViewCell.self, 
            forCellReuseIdentifier: 
                "TableViewCell") 
     self.tableView1.tableFooterView = UIView() 

     let items2 = [“1”,”2”,”3”] 
     dataSource2 = SecondDataSouce() 
     dataSource2.setData(items: items2) 
     self.recentTableView.dataSource = dataSource2 
     self.recentTableView.delegate = dataSource2 
     self.recentTableView.register(RecentTableViewCell.self, 
              forCellReuseIdentifier: 
                "TableViewCell") 
     self.recentTableView.tableFooterView = UIView() 
    } 
} 
Các vấn đề liên quan