3 回答

TA貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超11個贊
- (void)viewDidLoad{ [super viewDidLoad]; UINib *nib = [UINib nibWithNibName:@"ItemCell" bundle:nil]; [[self tableView] registerNib:nib forCellReuseIdentifier:@"ItemCell"];}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // Create an instance of ItemCell PointsItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemCell"]; return cell;}

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個贊
寄存器
// For registering nib filestableView.register(UINib(nibName: "MyCell", bundle: Bundle.main), forCellReuseIdentifier: "cell") // For registering classestableView.register(MyCellClass.self, forCellReuseIdentifier: "cell")
(注
)也可以通過在 .xib
或 .stroyboard
文件,作為原型單元格。如果需要將類附加到它們,可以選擇單元格原型并添加相應(yīng)的類(必須是 UITableViewCell
當(dāng)然)。
去隊列
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) cell.textLabel?.text = "Hello" return cell}
if (cell == nil)
(警告)
tableView.dequeueReusableCell(withIdentifier:for:)
有新的行為,如果你調(diào)用另一個(沒有 indexPath:
)你得到了舊的行為,在這種行為中你需要檢查 nil
如果你自己動手的話,注意到 UITableViewCell?
返回值
if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? MyCellClass{ // Cell be casted properly cell.myCustomProperty = true}else{ // Wrong type? Wrong identifier?}
UITableViewCell
配置
cellForRowAtIndexPath
合在一起
class MyCell : UITableViewCell{ // Can be either created manually, or loaded from a nib with prototypes @IBOutlet weak var labelSomething : UILabel? = nil}class MasterViewController: UITableViewController { var data = ["Hello", "World", "Kinda", "Cliche", "Though"] // Register override func viewDidLoad() { super.viewDidLoad() tableView.register(MyCell.self, forCellReuseIdentifier: "mycell") // or the nib alternative } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data.count } // Dequeue override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "mycell", for: indexPath) as! MyCell cell.labelSomething?.text = data[indexPath.row] return cell }}
- 3 回答
- 0 關(guān)注
- 784 瀏覽
添加回答
舉報