2013-04-02 31 views
5

Tôi có một UIViewController mà tại một số điểm phát triển một UITableView, và khi nó tôi chỉ cần init biến TableView dụ và thêm nó vào xem, nhưng tôi không chắc chắn làm thế nào để xử lý dequeueing của các tế bào để thêm vào xem ; Tôi cần một số nhận dạng tái sử dụng, nhưng tôi không chắc chắn làm thế nào để thiết lập nó.Lập trình thêm một UITableView - làm cách nào để đặt mã định danh tái sử dụng cho các ô?

Tôi phải làm gì trong phương pháp này?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"wot"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 

    return cell; 
} 
+0

Bạn cũng làm như vậy trong bất kỳ việc thực hiện 'cellForRowAtIndexPath' nào. Không có gì về cách bạn có được chế độ xem bảng thay đổi cách hoạt động của chế độ xem bảng. Mã bạn đã hiển thị là một khởi đầu hoàn hảo tốt. – matt

Trả lời

7

Sử dụng phương pháp này initWithStyle:reuseIdentifier

  1. kiểm tra nếu cell tồn tại
  2. Nếu không, thì bạn cần phải khởi tạo nó.

đang

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath*)indexPath 
{ 
    static NSString *cellIdentifier = @"wot"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 

    if (!cell) 
     cell = [[UITableViewCell alloc] initWithStyle: someStyle reuseIdentifier: cellIdentifier]; 

    return cell; 
} 
+0

từ iOS 5 trên bạn không cần phải kiểm tra '! Cell'. http://stackoverflow.com/questions/7946840/dequeuereusablecellwithidentifier-behavior-changed-for-prototype-cells –

+0

Vì không giống với IB, tôi có thể cung cấp cho tất cả các ô một bố cục cụ thể với UIViews trên đó (UILabel, UIImage, v.v.) Tôi phải tạo một UILabel và thêm nó vào subview của ô cho mỗi ô? –

+0

Bạn có thể tạo một lớp con của UITableViewCell và thực hiện thiết lập subview cụ thể ở đó. – MJN

0

Từ định tái sử dụng không cần phải được defined.In rõ ràng phương pháp cellForRowAtIndexPath các định nghĩa bạn đã bao gồm trong câu hỏi là đủ để làm việc với

cho Reference

Ví dụ

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *MyIdentifier = @"MyReuseIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]]; 
    } 
    Region *region = [regions objectAtIndex:indexPath.section]; 
    TimeZoneWrapper *timeZoneWrapper = [region.timeZoneWrappers objectAtIndex:indexPath.row]; 
    cell.textLabel.text = timeZoneWrapper.localeName; 
    return cell; 
} 
Các vấn đề liên quan