2016-10-19 2 views
-2

Мой стол просто имеет ряд с UIImageView. Когда я задаю изображение непосредственно как нижеуказанный путьЕсть ли способ игнорировать регистрацию элемента или класса для TableViewCell?

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

    NSString *identifier = @"cellidentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 

    // Configure the cell... 
    cell.imageView.image = [self.backgroundImages objectAtIndex:indexPath.row]; 

    return cell; 
} 

возвращает ошибка:

unable to dequeue a cell with identifier cellidentifier - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'.

Так интересно, есть ли способ игнорировать зарегистрировать перо или класс для UITableViewCell для того, чтобы сократите код в этом случае?

ответ

1

Если вы используете собственный класс для ячейки, то вы должны зарегистрировать класс (в viewDidLoad), используйте registerNib:forCellWithReuseIdentifier:, если ячейка выполняется в файле xib.

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 

еще написать cellForRowAtIndexPath Tableview делегата следующим образом:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 
    //Configure cell 
    return cell; 
} 
1

Вам просто нужно добавить одну строку в viewDidLoad, чтобы устранить ошибку

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellidentifier"]; 
} 
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Songcell"; // copy this identifier and paste it into your storyboard tableviewcell indetifier 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    UIImageView *IMAGEVIEWNAME = (UIImageView*)[cell viewWithTag:YOUR_TAG]; // give tag to your imageview from storyboard and access it here 

    IMAGEVIEWNAME.image = [UIImage imageNamed:@"YOURIMAGENAME"]; 
    return cell; 
}  

Дайте же имя в раскадровке tableviewcell идентификатор

cellidentifier с этим именем. я просто передаю свой сотовый идентификатор songcell.

I just give it here songcell at that part you give it **cellidentifier**

0

Поместите свой идентификатор соты в раскадровке, как показано ниже скриншоте.

enter image description here

Смежные вопросы