2014-09-10 24 views
5

Tôi mới học cách phát triển iOS và ngôn ngữ Swift, và đã cố gắng tìm ra cách tạo một tùy chỉnh UITableViewCell mà không cần sử dụng Storyboards/Interface Builder. Tôi muốn có thể hoàn thành mọi thứ trong mã Swift. Cho đến nay, tôi đã thực sự chỉ có thể tìm thấy những người sử dụng Interface Builder.Custom table view cell trong Swift, không có Storyboard

Điều tôi muốn có thể làm là tạo một ô có thể sử dụng lại có thể được khởi tạo trong bất kỳ chế độ xem bảng nào. Từ những gì tôi hiểu tôi sẽ có thể tạo một ô tùy chỉnh với chế độ xem phụ, dữ liệu whos có thể được đặt bởi dữ liệu đến của chế độ xem bảng. Đúng?

Hiện tại, tôi có một số UINavigationController với số được nhúng, được phân loại là UITableViewController. Sau một số hướng dẫn khác, tôi cũng đã học cách tạo một Struct và chuẩn bị dữ liệu kiểm tra cho ô xem bảng. Nhưng đó là về xa như tôi đã có thể nhận được.

// My Table View Controller 
class InventoryListViewController: MainTableViewController { 

    let viewTitle = "Inventory" 
    var inventoryItems = [InventoryItem]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.navigationItem.title = viewTitle.uppercaseString 

     self.inventoryItems = [ 
      InventoryItem(name: "White Bread", expiry: "2014-09-12"), 
      InventoryItem(name: "Mushrooms", expiry: "2014-09-15"), 
      InventoryItem(name: "Watermelon", expiry: nil), 
      InventoryItem(name: "Leftover Thai", expiry: "2014-09-15"), 
      InventoryItem(name: "Cheddar Cheese", expiry: "2014-09-12"), 
      InventoryItem(name: "Chicken Breasts", expiry: "2014-09-10"), 
      InventoryItem(name: "Paprika", expiry: nil), 
      InventoryItem(name: "Sour Cream", expiry: nil) 
     ] 

     // Right now this will fail without tableView:cellForRowAtIndexPath 
     self.tableView.reloadData() 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.inventoryItems.count 
    } 

} 

// Sub-classed UITableViewController 
class MainTableViewController: UITableViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.tableView.separatorColor = UIColor.clearColor() 
     self.tableView.contentInset = UIEdgeInsetsZero 
    } 

} 

// Data struct for cells 
struct InventoryItem { 
    let name: String 
    let expiry: String? 
} 

Tôi hiểu Swift vẫn còn rất mới và đó có thể là lý do tại sao tôi không thể tìm thấy quá nhiều nguồn lực cho chủ đề này, vì vậy nếu có ai có bất kỳ gợi ý tôi sẽ rất biết ơn sự giúp đỡ!

Trả lời

9

Bạn có thể sử dụng dưới mã cho các tế bào tùy chỉnh mà không cần đăng ký storyboard.Just class của bạn và sử dụng nó với tableViewController

//Cell.Swift 

import Foundation 
import UIKit 

class Cell : UITableViewCell{ 

    //Do whatever you want as exta customization 
} 

điều khiển của bạn viewDidLoad

override func viewDidLoad() { 
    super.viewDidLoad() 

    // register your class with cell identifier 
    self.tableView.registerClass(Cell.self as AnyClass, forCellReuseIdentifier: "Cell") 
} 


//cellForRowAtIndexPath 

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

      cell = Cell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") 
     } 
     //configure your cell custom code 
     cell.textLabel?.text = @"Your Label Text" 
     return cell! 
    } 
+0

Hmm. Điều này trông rất hứa hẹn, mặc dù khi tôi thử nó ra nó lỗi 'self.configureCell' với' 'MainTableViewController' không có một thành viên có tên 'configureCell''. Tôi sẽ làm gì sai ở đây? –

+0

Tôi vừa thêm 'confgureCell' vào chương trình để làm mã tùy chỉnh.Bạn có thể xóa nó và không cần nó. Bạn có thể viết mã của riêng bạn bất cứ điều gì bạn muốn làm như thiết lập nhãn. Tôi đang chỉnh sửa mã của tôi như để thiết lập nhãn 'cell.textLabel? .text = @" Văn bản nhãn của bạn "' – codester

+0

Ồ tôi hiểu. Cảm ơn bạn! Điều này dường như làm việc! Mặc dù, nếu bạn không nhớ giải thích thêm - làm cách nào để thêm chế độ xem mới (như nhãn) vào ô tùy chỉnh _within_ lớp 'Cell'? Tôi đã thử làm [this] (http://tny.cz/b83cf1bf) nhưng nó không hoạt động! –

Các vấn đề liên quan