Как загрузить XIB-файл для ячейки в таблице в iphone - PullRequest
6 голосов
/ 11 июля 2011

Я видел, что в сети есть много ресурсов по этому вопросу. Я должен загрузить другой файл XIB (UIViewContoller) для моей ячейки. Я разработал дизайн своей камеры и хочу загрузить этот дизайн в свою камеру.

Я написал этот код, но получаю исключение.

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

А вот мой код загрузки nib-файла

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;

Ответы [ 5 ]

14 голосов
/ 11 июля 2011

Вы должны использовать UITableViewCell, а не UIView в вашем файле XIB.

2 голосов
/ 26 декабря 2011

Вот решение, и оно может быть полезным для меня и дома для вас или кого-то еще.

 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;
            }
        }
1 голос
/ 31 декабря 2012
 - (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];
    }
 }
1 голос
/ 11 июля 2011

Хотя это и не прямой ответ на ваш вопрос, я рекомендую вам использовать этот класс UITableViewCellFactory, это очень удобно:

http://blog.carbonfive.com/2009/07/16/loading-uitableviewcells-from-a-nib-file/

0 голосов
/ 10 октября 2012

Это то, что я сделал.Опять же, некоторые приемы довольно неуклюжи, и я нуждаюсь в улучшении.

Сначала я создал новый подкласс UITableViewCell.Проблема в том, что я не могу проверить «включить» XIB.Как будто xib предназначен только для UIViewcontroller.Я полагаю, вы можете создать подкласс UIViewController с XIB, а затем создать другой подкласс 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 было собой, а не subView of self.Но хей .... Не знаю, как это сделать иначе....

Затем я реализую это для 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;
    }
}

Обратите внимание, что я отделяю alloc от init.Это немного неловко.Вот почему в моем - (BGCRBusinessForDisplay2 *) initWithBiz: (Business *) biz, если ячейка была инициализирована ранее, я просто не выполняю верхнюю часть инициализации.Я просто присваиваю значения Business * различным точкам в BGCRBusinessForDisplay2.

Я могу улучшить мои ответы, и они приветствуются.Пока это работает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...