Reputation: 24476
I'm trying to load the data UITableView
which is coming from webservice. Everything is working fine. But, whenver the record is being displayed in UITableView
using -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
delegate method i'm getting exception like below-
Here is my code -
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell; //= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Reuse_Cell"];
UILabel *label = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.selectedBackgroundView = [[UIView alloc]init];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
label = [[UILabel alloc]initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setNumberOfLines:1];
[label setFont:[UIFont fontWithName:@"Trebuchet MS" size:18]];
[label setTag:1];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
[[cell contentView] addSubview:label];
}
if (!label)
label = (UILabel *)[cell viewWithTag:1];
[label setText:[[webserviceRecords objectAtIndex:indexPath.row] objectForKey:@"catName"]];
[label setFrame:CGRectMake(10.0, 5.0, 225.0, 35.0)];
cell.selectedBackgroundView.backgroundColor = RGBACOLOR(151.0, 191.0, 69.0, 1.0);
cell.layer.borderWidth = 2.0f;
cell.layer.borderColor = RGBCOLOR(214, 218, 1).CGColor;
return cell;
}
My Array records -
Custom init
method
- (id)initwithInteger:(NSInteger )integer
{
self = [super init];
if (self) {
startingpage = integer;
webserviceRecords = [[NSMutableArray alloc]init];
}
return self;
}
NumberofRowsInSection
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"Row count - %d", [webserviceRecords count]);
return [webserviceRecords count];
}
Upvotes: 0
Views: 562
Reputation: 24476
Really a silly mistake was there. I've parsed the JSON
data directly into my NSMutableArray
I've parsed like below first -
webserviceRecords = [NSJSONSerialization JSONObjectWithData:mutableData options:0 error:&error];
Instead of this i've fixed this like below -
NSArray *array = nil;
array = [NSJSONSerialization JSONObjectWithData:mutableData options:0 error:&error];
webserviceRecords = [array mutableCopy];
It helps me to fix my issue. Thanks for responsing my question.
Cheers!
Upvotes: 1
Reputation: 2411
You're setting the cell's selectedBackgroundView
before checking that the cell has a value. You should check if the cell is nil first before doing that.
On a side note, you may find it much easier to display your array using the free Sensible TableView framework. The framework generates all the cells automatically from your array (including all the detail views), and can even fetch the data from the web service directly if you wish. Should save you a lot of headache and manual work. Hope this helps.
Upvotes: 1