2014-10-06 7 views
6

मैं एक दृश्य में दो तालिकाओं दो अलग सरणियों से आबादी प्रदर्शित करने के लिए निम्नलिखित कोड है:दो टेबल तेज

@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 
    } 

मैं पहली बार तालिका काम कर रहे है, और उसके बाद की नकल की और पाठ चिपकाया, सरणी नामों और तालिकादृश्य नामों को प्रतिस्थापित करना, और प्रतिनिधि और डेटास्रोत को जोड़ दिया है। हालांकि एक्सकोड दूसरे (चिपकाए गए) कोड पर 'viewdidload का अमान्य पुनर्विक्रय' प्रदर्शित करता है। यदि मैं इसे ऐप बिल्ड को देखने के बजाय 'फंड लोड व्यू() {' में बदल देता हूं। जब मैं इसका परीक्षण करता हूं, हालांकि, दोनों सारणी बिल्कुल उसी डेटा को देखते हैं जो 'RFArray' में डेटा है। मैं कोडिंग के लिए बहुत नया हूं और देख नहीं सकता कि मैंने क्या किया है, कृपया मदद करें।

उत्तर

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 
    } 
} 

बस एक त्वरित संपादन। आपको प्रतिनिधि और डेटासोर्स विधियों को समान रखने की आवश्यकता है और यह जांचें कि कौन सा टेबल व्यू इंस्टेंस वास्तव में संदेश भेज रहा है।

आप व्युत्पन्न कक्षा में दो बार एक ही विधि को ओवरराइड नहीं कर सकते हैं।

+1

आपको बहुत धन्यवाद, यह सही समझ में आया, स्पष्टीकरण के लिए धन्यवाद। पहली बार काम किया – samp17

0

टेबलव्यूसेल में डेटा लाने के बाद प्रत्येक तालिका दृश्य को फिर से लोड करने के लिए भी सुनिश्चित करें।

, उदा,

@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

सबसे पहले बनाने के दो डेटा स्रोत लागू किया कक्षाओं फर्स्ट डाटा स्रोत

ViewController में

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 
    } 
} 

दूसरा डेटा स्रोत tableview करने

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 
} 
} 

सेट डेटा स्रोत

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() 
    } 
}