2
I want to make a custom cell for my Tableview. But it is presenting problems that I am not able to solve. It worked normally when I did with the standard cell, but when I did custom gave problem
I was using this explanation as help: http://www.appcoda.com/customize-table-view-cells-for-uitableview/
I made my cell this way..
Simpletablecell.xib and Simpletablecell. h:
Simpletablecell. m
import "SimpleTableCell.h"
@interface SimpleTableCell ()
@end
@implementation SimpleTableCell
@synthesize nome = _nome;
@synthesize texto = _texto;
@synthesize data = _data;
@synthesize foto = _foto;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
My Viewcontroller (Where is Tableview), this way.
Viewcontroller. m
#import "ViewController.h"
#import "SimpleTableCell.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//________________________
_tabela.dataSource = self;
_tabela.delegate = self;
//________________________
//Na ViewDidLoad eu pego um Json e armazenando seus dados em uma lista (lista manda dados para célula)
_users = [[NSMutableArray alloc] init];
NSURLRequest *requisicao = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.meuSite.com.br/mural/listener.php?a=10"]];
NSData *resposta = [NSURLConnection sendSynchronousRequest:requisicao
returningResponse:nil
error:nil];
NSError *jsonParsingError = nil;
NSArray *array = [NSJSONSerialization JSONObjectWithData: resposta options: nil error: &jsonParsingError];
NSLog(@"result = %@", array.firstObject);
int total = [array count];
for (int x =0; x<total; x++) {
[_users addObject:[array objectAtIndex:x]];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{//tamanho da lista
return [_users count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSLog(@"QUANDO COMPILO SÓ VEM ATÉ AQUI. . .");
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
NSLog(@"%d", 23);
cell = [nib objectAtIndex:0];
NSLog(@"%d", 3);
}
NSDictionary * usuario = [_users objectAtIndex:indexPath.row];
cell.nome.text = [usuario objectForKey:@"nome"];
cell.texto.text = [usuario objectForKey:@"texto"];
cell.data.text = [usuario objectForKey:@"data"];
NSString *img = @"http://diegocavalca.com/wp-content/uploads/2016/01/iphone_6_home_screen_hero.jpg";
NSString *url_imagem = [NSString stringWithFormat:img];
NSLog(@"%@", url_imagem);
NSURL *url=[NSURL URLWithString:url_imagem];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
cell.foto.image =[UIImage imageWithData:data];
}];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
Viewcontroller. h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
@property NSMutableArray * users;
@property NSIndexPath * selecionado;
@property (weak, nonatomic) IBOutlet UITableView *tabela;
@end
Error Presented. . .
-[Nsnull length]: unrecognized selector sent to instance 0x381cf588' In case anyone knows how I can fix this, I thank you in advance.
The nib in case, is your interface file, which from what I understand is the
MuralCell.xib
, but your class is named afterSimpleTableCell
and it seems that the file name isMuralCellM.h
. Anyway, I was very confused about these names, and if so, this is the reason. The error says that it cannot load a "nib" of name Simpletablecell.– Paulo Rodrigues
Thank you Paulo, I’ll fix it right now! if I get or don’t notice soon.
– CristianCotrena
You were right, there was conflict between the names, I corrected here and in the question too. But now it takes all the information from JSON and after that, it presents an error stating the problem I updated in the question.
– CristianCotrena
Put a "breakpoint for all execptions" in the debug menu, then it stops at the line where the problem occurs. This way it is difficult to say what can be.
– Luis Henrique
It is no longer presenting errors, but when I have compiled, it does not show any information in the list. The screen is all blank. I’m working on it to see if I can fix it. The only thing that appears after the execution I left informed on (Errors Presented) in the question
– CristianCotrena
You need to implement the following method as well:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
– Luis Henrique
Thanks for the tip Luis, implemented at Viewcontroller. m but the problem mentioned earlier remained.
– CristianCotrena
Never worked with cell creating XIB, I do it by Interfacebuilder even... have you ever tried to debug your code line by line to see where you are logging this error? I would guess that it is time to read the json that is not finding any key, but it may be time to load the XIB as well. Anyway, just debugging to solve.
– Luis Henrique
The JSON are okay, Henrique, I was doing it now. I’m going to try to fix it tonight. Tomorrow I inform here if you have succeeded in any way find any solution, inform that the night I return here. Hug!
– CristianCotrena