2014-10-01 15 views
6

Tôi đang cố gắng thêm nhiều lớp con vào một UITableView. Vấn đề là nó giữ cho tôi lỗi sau:nhiều lớp con nhanh trong UITableViewCOntroller

Type UITableVieCell does not conform to protocol NilLiteralConvertible 

cellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if indexPath.section == 0 { 

     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 

     cell.textLabel?.text = self.section1[indexPath.row] 
     cell.accessoryType = .DisclosureIndicator 

     return cell 

    } else if indexPath.section == 1 { 

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

     cell.cellLabel?.text = self.section2[indexPath.row] 

     return cell 
    } 

    return nil 
} 

Trả lời

-3

Bạn không được phép trở về con số không cho cellForRowAtIndexPath

Bạn có thể sử dụng ở cuối:

let cell:UITableViewCell! 
return cell 

thay vì

return nil 

Điều này sẽ là (nếu bạn chỉ có 2 phần) không bao giờ được thực thi.

Edit:

Bạn cũng có thể sử dụng thay vì "} else if indexPath.section == 1 {" only} else {- để trả lại một tế bào. Tôi chỉ cho thấy vấn đề là gì. Hoặc sử dụng một Switch/Case và trả về một ô trống theo mặc định.

+0

Giải pháp này tạo ra một cảnh báo với Xcode 6: 'giá trị bất di bất dịch là mặc định khởi tạo và không bao giờ có thể được changed'. Hơn nữa, việc tạo ra một tùy chọn không được khai báo hoàn toàn được khởi tạo với nil và hy vọng không bao giờ được gọi để im lặng một thông báo lỗi cho một phương thức yêu cầu trả về một 'UITableViewCell' không bắt buộc có vẻ như thiết kế mã lỗi. –

+0

Ông cũng có thể sử dụng thay vì "} else nếu indexPath.section == 1 {" only} else {- để trả về một ô. Tôi chỉ cho thấy vấn đề là gì. Hoặc sử dụng một Switch/Case và trả về một ô trống theo mặc định. – derdida

6

tableView:cellForRowAtIndexPath: phải trả lại UITableViewCell và không thể trả lại nil. Vì vậy, bạn sẽ phải xóa return nil. Nhưng nó sẽ không đủ. Tuyên bố if else của bạn cũng phải hoàn tất. Điều đó có nghĩa là mọi giá trị có thể có của section phải được cung cấp hoặc ít nhất, gửi đến một lần xuất hiện.

tuyên bố if else của bạn sẽ trông như thế này:

if indexPath.section == 0 { 
    /* ... */ 
} else if indexPath.section == 1 { 
    /* ... */ 
} else { 
    /* ... */ 
} 

Hoặc như thế này:

if indexPath.section == 0 { 
    /* ... */ 
} else { 
    /* ... */ 
} 

Tuy nhiên, sau đây if else tuyên bố là không hoàn thành:

if indexPath.section == 0 { 
    /* ... */ 
} else if indexPath.section == 1 { 
    /* ... */ 
} 

Trong trường hợp này , tableView:cellForRowAtIndexPath: sẽ không biết phải trả lại gì nếu có o f các điều kiện này được xác minh. Vì vậy, nếu bạn thử nó, Xcode (có nghĩa là thông minh) cũng sẽ hiển thị một thông báo lỗi:

Missing return in a function expected to return 'UITableViewCell'

Do đó, các mã sau đây nên làm việc:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if indexPath.section == 0 { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 

     cell.textLabel?.text = self.section1[indexPath.row] 
     cell.accessoryType = .DisclosureIndicator 

     return cell 
    } else { 
     let cell = tableView.dequeueReusableCellWithIdentifier("SwitchViewCell", forIndexPath: indexPath) as SwitchViewCell 

     cell.cellLabel?.text = self.section2[indexPath.row] 

     return cell 
    } 
} 

PS:

Nếu bạn cũng sử dụng câu lệnh switch bên trong phương thức tableView:cellForRowAtIndexPath: để đặt các ô của mình, this answer to a similar question có thể giúp bạn.

8

Bạn không thể trả lại số không. Thay vào đó, trả về ô trống rỗng mặc định.

var cell :UITableViewCell! 

    switch (indexPath.row) { 
    case 0: 
     cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 
     cell.textLabel?.text = self.section1[indexPath.row] 
     cell.accessoryType = .DisclosureIndicator 
     break; 

    case 1: 
     cell = tableView.dequeueReusableCellWithIdentifier("SwitchViewCell", forIndexPath: indexPath) as SwitchViewCell 
     (cell as SwitchViewCell).cellLabel?.text = self.section2[indexPath.row] 
     break; 

    default: 
     break; 
    } 

    return cell 

Hãy chắc chắn rằng bạn đã đăng ký nib

self.tableView.registerNib(UINib(nibName: "SwitchViewCell", bundle: nil), forCellReuseIdentifier: "SwitchViewCell") 
Các vấn đề liên quan