2011-07-11 13 views
6

この質問に関してネット上で利用可能なリソースがたくさんあります。自分のセルに別のXIBファイル(UIViewContoller)をロードする必要があります。私は自分のセルの外観をデザインして、そのデザインをセルにロードしたいと思っています。iphoneでtableviewのセルにxibファイルをロードするには

私はこのコードを書いていますが、例外が発生しています。

-[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' 

そして、ここではあなたがあなたのXIBファイルでUITableViewCellなくUIViewを使用する必要があります

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/これは良いチュートリアルですこの質問のために。 –

答えて

14

ロードnibファイルの私のコードです。

+1

+1私を助けました。なぜこの人々は正解を受け入れないのですか。 –

2

は溶液であり、それは私とあなたのための家のために有用です他の人がいるかもしれません。

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

これは私がしたことです。また、技術のいくつかはかなり厄介で、私は改善が必要です。

まず、私はUITableViewCellの新しいサブクラスを作成しました。問題は、 "include" xibをチェックするオプションがないことです。これは、xibがUIViewcontrollerだけを意味するかのようです。私はあなたがXIBでUIViewControllerのサブクラスを作成し、UITableViewCellの別のサブクラスを作成し、UIViewControllerのサブクラスにテンプレートを移動することができると思います。

作品。

は、それから私は、これらの機能を置く:

@implementation BGCRBusinessForDisplay2 

- (NSString *) reuseIdentifier { 
    return [[self class] reuseIdentifier]; 
}; 
+ (NSString *) reuseIdentifier { 
    return NSStringFromClass([self class]); 
}; 

私が行う初期化するには:

- (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; 

[self addSubview:self.view];が厄介なの一種です。それは他人が私がすべきことを言い、それなしではうまくいかないと言います。実際には、自己のサブビューではなく、self.viewを自己にしたいと思っています。しかし、hei ....それ以外の方法を知らないでください。 ...

それから私は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; 
    } 
} 

私はINITからのallocを個別に通知するためにこれを実装します。それは一種の厄介です。それで私の - (BGCRBusinessForDisplay2 *) initWithBiz: (Business *) bizで、セルが以前に初期化されていた場合、私は単にinitの上部をしません。私は単にビジネス*の値をBGCRBusinessForDisplay2のさまざまな店舗に割り当てます。

誰かが私の回答を改善することができます。これまでのところ動作します。

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]; 
    } 
} 
関連する問題