2015-12-10 15 views
6

Đang cố gắng giải quyết một vài cảnh trong ứng dụng của tôi, tôi không thể nhận được tableViews hoạt động như mong muốn.Lỗi với Chế độ xem bảng tĩnh

Im thực hiện cài đặt như tableView nơi người dùng chọn giữa hai cài đặt có thể. Mỗi tùy chọn dẫn đến tableView mới, nơi người dùng phải chọn một trong các tùy chọn có sẵn. Khi người dùng đã chọn một nhãn, nhãn trong cảnh đầu tiên sẽ hiển thị giá trị được chọn (được lưu trữ dưới dạng NSUserDefaults). Vì vậy, nếu Im không nhầm lẫn điều này có thể đạt được với 3 tableViews tĩnh. Các mã sau đây là cách Im cố gắng để làm điều đó:

thiết lập ban đầu xem điều khiển:

class SettingsVC2: UITableViewController{ 
override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
} 
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 2 
} 
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    if indexPath.section == 0{ 

     let cell = tableView.dequeueReusableCellWithIdentifier("office_cell", forIndexPath: indexPath) 
     cell.textLabel?.text = "Select your office" 
     return cell 
} 
    else { 
     let cell = tableView.dequeueReusableCellWithIdentifier("dept_cell", forIndexPath: indexPath) 
     cell.textLabel?.text = "Select your department" 
     return cell 
    } 

Một trong các thiết lập tableViews:

class SettingsDepartmentTVC: UITableViewController{ 

let departamentos: [String] = ["Sales", "Pre-Sales", "General Administration", "Customer Service", "Profesional Services", "Regionales"] 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return departamentos.count 
} 
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 

} 
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 
    let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("department_cell", forIndexPath: indexPath) 
    cell.textLabel?.text = self.departamentos[indexPath.row] 
    return cell 

} 
} 

Tôi tin rằng tất cả datasources và các đại biểu là cuốn hút ngay và UIKit được nhập ngay cả khi không được hiển thị.

erros Im nhận: thất bại

Assertion trong - [UITableView dequeueReusableCellWithIdentifier: forIndexPath:]

Chấm dứt ứng dụng do ngoại lệ uncaught 'NSInternalInconsistencyException', lý do: 'không thể dequeue một tế bào với số nhận dạng office_cell - phải đăng ký một ngòi hoặc một lớp cho số nhận dạng hoặc kết nối một ô mẫu trong bảng phân cảnh


Đây là phương pháp mà tôi cố gắng để cư tableView:

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


    let cell = tableView.dequeueReusableCellWithIdentifier("CommentsRowTVC", forIndexPath: indexPath) as! CommentsRowTVC 

    let single_comment = self.AOS[indexPath.row] 

    cell.titulo_comentario?.text = single_comment.titulo_comment 
     cell.cuerpo_comentario?.text = single_comment.cuerpo_comment 
    cell.fecha_comentario?.text = single_comment.fecha_comment 
    return cell 
} 

class CommentsRowTVC: UITableViewCell { 


@IBOutlet weak var titulo_comentario: UILabel! 
@IBOutlet weak var cuerpo_comentario: UILabel! 
@IBOutlet weak var fecha_comentario: UILabel! 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 

}

Dưới đây là các kết nối thanh tra cho UIViewController nơi bàn là:

Outlets

Và cái này cho UIViewCellController:

Outlets2

lỗi đến thời điểm này:

UIView tableView: numberOfRowsInSection: chọn không được công nhận gửi đến dụ

Chấm dứt ứng dụng do ngoại lệ uncaught NSInvalidArgumentException, lý do: [UIView tableView: numberOfRowsInSection:] : bộ chọn không được công nhận được gửi đến ví dụ

+0

bạn có thể thêm một ảnh chụp màn hình Storyboard? – adolfosrs

+0

bạn đã thêm bộ thụt lề vào bảng phân cảnh của mình chưa? – adolfosrs

+0

@PabloDuque bạn nên đặt một điểm ngắt và thử bước qua mã của bạn, xem nơi nó phá vỡ và báo cáo lại –

Trả lời

2

Nếu bạn thực sự đang thực hiện static các ô xem bảng, sau đó bạn chỉ cần xóa các phương thức này. Bạn chỉ sử dụng chúng khi bạn đang dequeueing tế bào động.

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return departamentos.count 
} 
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 

} 
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 
    let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("department_cell", forIndexPath: indexPath) 
    cell.textLabel?.text = self.departamentos[indexPath.row] 
    return cell 

} 

Nếu bạn đang thực sự cố gắng để làm tế bào động sau đó, như thông báo lỗi của bạn gợi ý, bạn cần phải chắc chắn nhận dạng của bạn phù hợp với những gì bạn có trong xây dựng giao diện:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 
    let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("{your_identifier}", forIndexPath: indexPath) 
    cell.textLabel?.text = self.departamentos[indexPath.row] 
    return cell 

} 
+0

Làm cách nào để thêm nội dung vào các ô? –

+1

Nếu bạn đang tạo các ô tĩnh, bạn thực hiện nó trong bảng phân cảnh. Chỉ cần kéo nhãn ra và đặt tên cho chúng là những gì bạn muốn. "Tĩnh" có nghĩa là chúng không bao giờ thay đổi, vì vậy chỉ cần kéo nhãn ra sẽ thực hiện thủ thuật nhưng chỉ khi bạn loại bỏ các phương pháp tôi đã đề cập. –

+0

Được rồi, tôi hiểu. Điều gì về xem bảng mà nhãn hiển thị những gì được lưu trữ trong NSUserDefautls? Sẽ là một cái gì đó giống như những gì tôi đã cố gắng để làm việc? –

1

Với sự mã bạn đã cung cấp có vẻ như bạn chưa đăng ký các ô và số nhận dạng bảng của bạn với chế độ xem bảng.

UITableView Class Reference

Thảo luận Trước dequeueing bất kỳ tế bào, gọi phương thức này hoặc registerNib: forCellReuseIdentifier: Phương pháp để nói với xem bảng cách để tạo ra các tế bào mới. Nếu một ô thuộc loại được chỉ định hiện không phải là trong hàng đợi sử dụng lại, chế độ xem bảng sử dụng thông tin được cung cấp để tự động tạo một đối tượng ô mới.

Nếu trước đây bạn đã đăng ký một lớp hoặc tệp nib với cùng một lần sử dụng số nhận dạng, lớp bạn chỉ định trong tham số cellClass sẽ thay thế mục nhập cũ. Bạn có thể chỉ định nil cho cellClass nếu bạn muốn hủy đăng ký lớp khỏi định danh tái sử dụng được chỉ định.

Tôi khuyên bạn nên đăng ký chúng trong viewDidLoad().

let KDepartmentCellIdentifier = "department_cell" 
    tableView.registerClass(UITableViewCell.class, forCellReuseIdentifier: kKDepartmentCellIdentifier) 

    let KOfficeCellIdentifier = "office_cell" 
    tableView.registerClass(UITableViewCell.class, forCellReuseIdentifier: kOfficeCellIdentifier) 

Nếu bạn đang sử dụng một ngòi sau đó sử dụng

registerNib(_ nib: UINib?, forCellReuseIdentifier identifier: String) 
Các vấn đề liên quan