uitableviewcell source number example data ios objective-c cocoa-touch uitableview

ios - source - Fallo de aserción en UITableView configureCellForDisplay: forIndexPath:



uitableviewcell swift 3 (6)

No estoy seguro de dónde está el error aquí, después de haber visto otros problemas similares. Recibí un error de aserción.

Terminating app due to uncaught exception ''NSInternalInconsistencyException'', reason: UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:

Creo que es algo simple, pero espero que alguien pueda ayudar.

A continuación está mi código:

#import "StockMarketViewController.h" @interface StockMarketViewController () @end @implementation StockMarketViewController @synthesize ShareNameText, ShareValueText, AmountText; @synthesize shares, shareValues; - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; { return [shares count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; { UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; NSString *currentValue = [shareValues objectAtIndex:[indexPath row]]; [[cell textLabel]setText:currentValue]; return cell; }


Debe llamar a "initWithStyle" en TableViewCell personalizado e inicializar los objetos nuevamente.

Ejemplo: archivo ProductTableViewCell.m

@implementation ProductTableViewCell - (void)awakeFromNib { } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; } - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { self.selectionStyle = UITableViewCellSelectionStyleNone; _titleLabel = [[UILabel alloc] initWithFrame:(CGRectMake(70, 0, 320, 60))]; [self.contentView addSubview:_titleLabel]; } return self; }

En el archivo de implementación principal

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ ProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"productTableViewCell"]; NSDictionary *dic = nil; if (tableView == self.searchDisplayController.searchResultsTableView) { dic = [_filteredArray objectAtIndex:indexPath.row]; } else { dic = [_originalArray objectAtIndex:indexPath.row]; } cell.titleLabel.text = [dic objectForKey: @"title"]; return cell; }


En el siguiente código ha escrito @"cell" (escrito con una pequeña c ), pero tiene que usar @"Cell" ( C debe ser mayúscula).

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


Si no ha definido un prototipo de celda con el identificador @"cell" en Storyboard, obtendrá un error de afirmación cuando intente dequearlo.

Puede solucionar esto estableciendo la propiedad Identifier en la celda del prototipo (seleccione la celda y establezca ese atributo en el panel de la derecha).


Tuve el mismo error y logré encontrar la falla. Tenía una matriz para los títulos y ver títulos:

NSArray *MMTitles= [NSArray arrayWithObjects:@"MainMenu",@"viewIt",@"viewNots",@"MyProfile",@"Settings",@"Instructions",@"Help", nil]; NSArray *MMSegues=[NSArray arrayWithObjects:@"MainMenu",@"MyProfileSegue",@"viewNotSegue",@"MyProfileSegue",@"SettingsTableViewSegue",@"InstructionsViewSegue",@"HelpViewSegue", nil]; self.menuItems = [[NSArray alloc]initWithObjects:MMTitles,MMSegues, nil];

Luego utilicé esta matriz como fuente de datos para mi tabla. El error que estaba recibiendo se debía al hecho de que, de hecho, no había declarado HelpViewSegue en mi Storyboard cuando instalé el VC:

vc = [mainStoryboard instantiateViewControllerWithIdentifier: [[self.menuItems objectAtIndex:1]objectAtIndex:indexPath.row]];

¡Bastante trivial, pero fue bastante frustrante! Espero que esto haya ayudado.


Un error muy tonto que había hecho era

No puse el UITableViewDelegate, UITableViewDataSource después de que el nombre de la clase del controlador como mi código de clase fuera clase TagsViewController: UIViewController

debería tener la clase TagsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

Puede ser que uno de ustedes esté enfrentando debido a que todo el otro código estaba bien.


nunca creas una celda, solo intentas reutilizar una celda eliminada. pero como nunca creaste uno, no hay ninguno.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; { static NSString *cellIdentifier = @"cell"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; } NSString *currentValue = [shareValues objectAtIndex:[indexPath row]]; [[cell textLabel]setText:currentValue]; return cell; }

o prueba (solo iOS 6+)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; { static NSString *cellIdentifier = @"cell"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; NSString *currentValue = [shareValues objectAtIndex:[indexPath row]]; [[cell textLabel]setText:currentValue]; return cell; }

de UITableView.h

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one. - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered

-dequeueReusableCellWithIdentifier: siempre necesitará una verificación, si se devolvió una celda, mientras
-dequeueReusableCellWithIdentifier:forIndexPath: puede instanciar uno nuevo.