Reputation: 807
I'm using Objective-C. I want to push a view controller with a table view in it. When I use a normal table view cell, it works well. But when I use custom cells, It couldn't load and the simulator freeze.
Is there any problem can cause the simulator freeze when push a view controller? Some one can help me?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *simpleIdentifier = @"comment";
commentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if (cell == nil){
cell = [[commentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentifier];
}
cell.username.text = [comments[indexPath.row] objectForKey:@"user"];
cell.textLabel.text = [comments[indexPath.row] objectForKey:@"content"];
return cell;
}
Upvotes: 0
Views: 148
Reputation: 9589
in ViewController
.h
@property (strong, nonatomic) IBOutlet UITableView *tableViewData;
.m
#import "ViewController.h"
#import "CustomCell.h"
@interface ViewController ()
{
NSDictionary *dictComments;
NSArray *arrayUser;
NSArray *arrayContents;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
dictComments = [[NSDictionary alloc]initWithObjectsAndKeys:@"Jin",@"User",@"iOS Developer",@"Content", nil];
arrayUser = [[NSArray alloc]initWithObjects:[dictComments objectForKey:@"User"], nil];
arrayContents = [[NSArray alloc]initWithObjects:[dictComments objectForKey:@"Content"], nil];
}
//UITableViewDataSource Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrayUser.count;
//OR
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customTableIdentifier=@"CustomCell";
CustomCell *cell=(CustomCell*)[tableView dequeueReusableCellWithIdentifier:customTableIdentifier];
if (cell==nil)
{
NSArray *nibs=[[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil];
cell=[nibs objectAtIndex:0];
}
cell.labelUserName.text = [NSString stringWithFormat:@"%@",[arrayUser objectAtIndex:indexPath.row]];
cell.labelTextContent.text = [NSString stringWithFormat:@"%@",[arrayContents objectAtIndex:indexPath.row]];
//OR
cell.labelUserName.text = [NSString stringWithFormat:@"%@",[dictComments objectForKey:@"User"]];
cell.labelTextContent.text = [NSString stringWithFormat:@"%@",[dictComments objectForKey:@"Content"]];
return cell;
}
Upvotes: 0
Reputation: 864
try these way..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *cellidentifier=[NSString stringWithFormat:@"%ld",(long)indexPath.row];
CustomCell *cell= (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if (cell==nil)
{
cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
if (indexPath.row==0)
{
//Here change or edit specific custom cell if u want like i have UIImageview and i want to give it a specific image..
[cell.imgView1 setImage:imgsp1];
}
return cell;
}
i hope it helps..
Upvotes: 0
Reputation: 1
You need to register your custom cell in viewdidload
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[tableView registerNib:[UINib nibWithNibName:@"myCustomCell" bundle:nil] forCellWithReuseIdentifier:@"myCell"];
}
Upvotes: 0
Reputation: 560
#pragma mark TableView delegete methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [Array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"cellIdentifier";
SocialTableViewCell *cell=(SocialTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SocialTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//write code for display data
return cell;
}
Upvotes: 0
Reputation: 9589
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customTableIdentifier=@"CustomCell";
CustomCell *cell=(CustomCell*)[tableView dequeueReusableCellWithIdentifier:customTableIdentifier];
if (cell==nil)
{
NSArray *nibs=[[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil];
cell=[nibs objectAtIndex:0];
}
cell.yourLabelData.text = @"iOS";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) //If you want to go view controller from particular index to other View Controller
{
//Here write the code for push the view
}
}
Upvotes: 1