2011-07-11 19 views
6

Tôi thấy có rất nhiều tài nguyên có sẵn trên mạng về câu hỏi này. Tôi phải tải một tệp XIB (UIViewContoller) khác cho ô của mình. Tôi đã thiết kế tế bào của tôi trông ở đó và tôi muốn tải thiết kế đó trong tế bào của tôi.Cách tải tệp xib cho ô trong chế độ xem bảng trong iphone

Tôi đã viết mã này nhưng tôi nhận được một ngoại lệ.

-[UIView setTableViewStyle:]: unrecognized selector sent to instance 0x6040de0 
2011-07-11 14:42:27.461 TableViewTest[2776:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setTableViewStyle:]: unrecognized selector sent to instance 0x6040de0' 

Và đây là mã của tôi về tập tin nib tải

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    // Load the top-level objects from the custom cell XIB. 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"loadXibfile" owner:self options:nil]; 
    // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). 
    cell = [topLevelObjects objectAtIndex:0]; 
} 


return cell; 
+0

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. –

Trả lời

14

Bạn nên sử dụng UITableViewCell và không UIView trong tập tin XIB của bạn.

+1

+1 Đã giúp tôi. tại sao người này không chấp nhận câu trả lời đúng. –

2

Dưới đây là giải pháp và nó rất hữu ích cho tôi và gia đình cho bạn hoặc một số người khác có thể.

cell = [[[YourCustomcell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"] autorelease]; 
     NSArray *toplavelobject=[[NSBundle mainBundle]loadNibNamed:@"YourCustomcell" owner:self options:nil]; 
     for(id c in toplavelobject) 
     { 
      if ([c isKindOfClass:[UITableViewCell class]]) 
      { 
       cell=(YourCustomcell *) c; 
       break; 
      } 
     } 
0

Đâ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.

1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row]; 
    LatestNewsCell *cell = (LatestNewsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     cell = [[[NSBundle mainBundle] loadNibNamed:@"LatestNewsCell" owner:self options:nil] objectAtIndex:0]; 
    } 
} 
Các vấn đề liên quan