2015-09-29 2 views
0

Я установил delegate & datasource из Tableview в быстром и номер секции возвратного правильного значения, но numberOfRowsInSection всегда возвращает 4. Так я два вопросnumberOfRowsInSection всегда возвращает значение починки в быстрой

  1. Я не» я знаю, почему он возвращается 4 всегда.
  2. Когда число разделе 5, то почему numberOfRowsInSection вызывается множественным время, которое больше чем 5.

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
        println("No of Sections: \(self.contactArr.count)") 
    
        if self.contactArr.count > 0{ 
         return self.contactArr.count 
        } 
        else{ 
         return 0 
        } 
    } 
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
        println("Current Section \(section)") 
        var contactDic : NSDictionary  = self.contactArr.objectAtIndex(section) as! NSDictionary 
        var lcontactArr : NSMutableArray = contactDic["contact"] as! NSMutableArray 
    
        return lcontactArr.count; 
    } 
    

Выходной

No of Sections: 5 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 
Current Section 4 

Мой Контакт массив является :

Contacts =   (
         { 
       contact =     (
             { 
         date = "2015-09-07 05:00:30"; 
         message = "Dear Dr. sund, Everything is going well, I can't give you an expected finish time yet."; 
         name = sund; 
         prefix = "<null>"; 
         "user_messsage_id" = 3; 
        } 
       ); 
      }, 
         { 
       contact =     (
             { 
         date = "2015-09-07 05:38:23"; 
         message = "Dear Dr. Krishan Kumar Sharma, We are in the Recovery Room, everything went well."; 
         name = "Krishan Kumar Sharma"; 
         prefix = "Dr."; 
         "user_messsage_id" = 4; 
        }, 
             { 
         date = "2015-09-07 07:51:57"; 
         message = "Dear Mr. Krishan Kuram Sharma, In the Recovery Room, everything went well, someone will speak with you soon."; 
         name = "Krishan Kumar Sharma"; 
         prefix = "Dr."; 
         "user_messsage_id" = 4; 
        } 
       ); 
      }, 
         { 
       contact =     (
             { 
         date = "2015-09-22 02:37:21"; 
         message = "Dear Mr. xyz, Everything is going well, I can't give you an expected finish time yet."; 
         name = xyz; 
         prefix = "Mr."; 
         "user_messsage_id" = 19; 
        } 
       ); 
      }, 
         { 
       contact =     (
             { 
         date = "2015-09-28 09:28:26"; 
         message = "Dear Mr. tester, Everything is going well, I can't give you an expected finish time yet."; 
         name = tester; 
         prefix = "Mr."; 
         "user_messsage_id" = 20; 
        }, 
             { 
         date = "2015-09-28 09:31:39"; 
         message = "Dear Mr. tester, Everything is going well, we expect to finish within the next hour."; 
         name = tester; 
         prefix = "Mr."; 
         "user_messsage_id" = 20; 
        }, 
             { 
         date = "2015-09-28 09:32:01"; 
         message = "Dear Mr. tester, We are in the Recovery Room, everything went well."; 
         name = tester; 
         prefix = "Mr."; 
         "user_messsage_id" = 20; 
        } 
       ); 
      }, 
         { 
       contact =     (
             { 
         date = "2015-09-29 03:08:33"; 
         message = "Dear Mr. Jackson, Everything is going well, I can't give you an expected finish time yet."; 
         name = Jackson; 
         prefix = "<null>"; 
         "user_messsage_id" = 24; 
        } 
       ); 

Любая помощь была бы оценена.

+1

Вы проверили ваш lcontactArray? Возможно, он всегда возвращает такое же значение. – ridvankucuk

+0

Нет, я только что напечатал текущее значение раздела, чтобы оно было как 0, 1, 2 –

+0

Вы префикс 'override' в своих функциях datasource/delegate таблицы? – Abhinav

ответ

0

После настройки некоторых структур данных & тип каста в вашем коде, я попробовал образец POC, и он отлично работает для меня. Не могли бы вы попробовать с этим кодом один раз.

import UIKit 

class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource { 

var tableView: UITableView = UITableView() 

var contactDict1 : Dictionary = ["contact" : ["C1", "C2", "C3", "C4"]] 
var contactDict2 : Dictionary = ["contact" : ["C11", "C21", "C31", "C41"]] 
var contactDict3 : Dictionary = ["contact" : ["C12", "C22", "C32", "C42"]] 
var contactDict4 : Dictionary = ["contact" : ["C13", "C23", "C33", "C43"]] 
var contactDict5 : Dictionary = ["contact" : ["C14", "C24", "C34", "C44"]] 

var contactArr : [Dictionary<String, Array<String>>] = [] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    contactArr = [contactDict1, contactDict2, contactDict3, contactDict4, contactDict5] 

    // Do any additional setup after loading the view, typically from a nib. 

    tableView.frame   = self.view.frame 
    tableView.delegate  = self 
    tableView.dataSource = self 

    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 

    self.view.addSubview(tableView) 
} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    print("No of Sections: \(self.contactArr.count)") 

    if self.contactArr.count > 0 { 
     return self.contactArr.count 
    } 
    else{ 
     return 0 
    } 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    print("Current Section \(section)") 

    let contactDic = self.contactArr[section] as Dictionary 

    print("contactDic = \(contactDic)") 

    let lcontactArr = contactDic["contact"]! as Array<String> 

    return lcontactArr.count; 
} 


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell")! 

    let contactDic = self.contactArr[indexPath.section] as Dictionary 
    let lcontactArr = contactDic["contact"]! as Array<String> 

    cell.textLabel?.text = lcontactArr[indexPath.row] as String 

    return cell 
} 
}