Đây là những gì tôi đã làm. Một lần nữa một số kỹ thuật là khá khó xử và tôi cần cải tiến.
Trước tiên, tôi đã tạo một lớp con mới của UITableViewCell. Vấn đề là tôi không nhận được một tùy chọn để kiểm tra "bao gồm" xib. Nó giống như xib chỉ có nghĩa là cho UIViewcontroller. Tôi cho rằng bạn có thể tạo một phân lớp của UIViewController với XIB và sau đó crate một lớp con của UITableViewCell và di chuyển các mẫu để phân lớp của bạn UIViewController.
Hoạt động.
Sau đó, tôi đặt những chức năng:
@implementation BGCRBusinessForDisplay2
- (NSString *) reuseIdentifier {
return [[self class] reuseIdentifier];
};
+ (NSString *) reuseIdentifier {
return NSStringFromClass([self class]);
};
Để khởi tạo tôi làm:
- (BGCRBusinessForDisplay2 *) initWithBiz: (Business *) biz
{
if (self.biz == nil) //First time set up
{
self = [super init]; //If use dequeueReusableCellWithIdentifier then I shouldn't change the address self points to right
NSString * className = NSStringFromClass([self class]);
PO (className);
[[NSBundle mainBundle] loadNibNamed:className owner:self options:nil];
[self addSubview:self.view]; //What is this for? self.view is of type BGCRBusinessForDisplay2. That view should be self, not one of it's subview Things don't work without it though
}
if (biz==nil)
{
return self; //Useful if we only want to know the height of the cell
}
self.biz = biz;
self.Title.text = biz.Title; //Let's set this one thing first
self.Address.text=biz.ShortenedAddress;
Các [self addSubview:self.view];
là loại khó xử. Đó là những gì người khác nói tôi nên làm và nó sẽ không hoạt động nếu không có nó. Trên thực tế tôi muốn self.view là tự, chứ không phải là một subView của bản thân. Nhưng hei .... Không biết làm thế nào để làm điều đó nếu không. ...
Sau đó, tôi thực hiện điều này cho cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//[FetchClass singleton].FetchController
if([BGMDCRFetchClass singleton].FetchController.fetchedObjects.count!=0){
BGCRBusinessForDisplay2 *cell = (BGCRBusinessForDisplay2*)[tableView dequeueReusableCellWithIdentifier:[BGCRBusinessForDisplay2 reuseIdentifier]];
if (cell == nil)
{
cell =[BGCRBusinessForDisplay2 alloc];
}
else{
while (false);
}
Business * theBiz=[[BGMDCRFetchClass singleton].FetchController objectAtIndexPath:indexPath];
cell = [cell initWithBiz:theBiz];
return cell;
//return theBiz.CustomCell;
}else{
UITableViewCell * tvc=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tvc"];
return tvc;
}
}
Chú ý rằng tôi tách alloc từ init. Đó là loại vụng về. Đó là lý do tại sao trong của tôi - (BGCRBusinessForDisplay2 *) initWithBiz: (Business *) biz
nếu một ô đã được khởi tạo trước đó, tôi chỉ đơn giản là không làm phần trên của init. Tôi chỉ đơn giản gán các giá trị của Business * cho các cửa hàng khác nhau trong BGCRBusinessForDisplay2.
Tôi có thể cải thiện câu trả lời của mình. Cho đến nay nó hoạt động.
Nguồn
2012-10-10 01:36:21
http://adeem.me/blog/2009/05/30/iphone-sdk-tutorial-part-6-creating-custom-uitableviewcell-using-interface-builder-uitableview/ Đây là hướng dẫn tốt cho câu hỏi này. –