2012-01-20 28 views
5

Tôi có 2 lớp kế thừa UITableViewControllers. Và hai khung nhìn bảng này nên sử dụng cùng một UITableViewCell tùy chỉnh. Vậy làm thế nào tôi có thể sử dụng Custom UITableViewCell từ một tệp Nib trong hai lớp khác nhau? Chủ sở hữu của xib chỉ có thể là 1 lớp.iPhone: Cách sử dụng UITableViewCell trong hai chế độ xem bảng

@interface Class1 : UITableViewController<UITableViewDataSource, 
    UITableViewDelegate> { 
UITableViewCell *myCustomTableRow; 
} 

@property (nonatomic, retain) IBOutlet UITableViewCell *myCustomTableRow; 





@interface Class2 : UITableViewController<UITableViewDataSource, 
    UITableViewDelegate> { 
UITableViewCell *myCustomTableRow; 
} 

@property (nonatomic, retain) IBOutlet UITableViewCell *myCustomTableRow; 

Trả lời

3

Tạo một sub-class UITableViewController nói CommonTableView. Làm điều này với tư cách là chủ sở hữu của tệp nib. Sau đó kế thừa hai lớp bạn muốn triển khai từ CommonTableView Lớp. Điều này sẽ làm việc tốt.

+0

Đúng, ý tưởng hay..Thanks – Jim

0

Dựa trên quan điểm của tôi, Bạn nên có để tạo ra tế bào Custom và và cho bảng khác mà bạn cần phải tạo chỉ XIB cho điều đó và sử dụng .h và file .m cùng ...

chỉ điều khiển sự thay đổi trong XIB ...

Hy vọng bạn hiểu ..

Xem này How to create Customcellthis

+0

Tôi không hiểu. Bạn muốn tạo lớp kế thừa từ UITableViewCell (Lats đặt tên là CustomCellClass) và lớp đó sẽ là chủ sở hữu cho xib? Và trong Class1 và Class2 chỉ cần tạo ra thể hiện của CustomCellClass? – Jim

0

Sử dụng UINib class . Nó cho phép cho instantiation mà không có cha mẹ, và cũng cải thiện hiệu suất của bộ nhớ đệm nib nội dung tập tin.

@interface Class1 : UITableViewController<UITableViewDataSource, UITableViewDelegate> { 
    UINib *myCustomCellNib; 
} 

... và điều tương tự cho Class2. Sau đó, trong viewDidLoad

myCustomCellNib = [UINib nibWithNibName:@"myCustomCellNibName" bundle:nil]; 

sau đó, khi tạo tế bào:

NSArray* objects = [myCustomCellNib instantiateWithOwner:nil options:nil]; 
// given that your cell is defined as a first object in your nib 
cell = [objects objectAtIndex:0]; 
Các vấn đề liên quan