0

I'm trying to load the data UITableViewwhich 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-

enter image description here

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 -

http://pastie.org/7120406

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];
}
35
  • 1
    mark a breakpoint and step into.... Commented Mar 26, 2013 at 5:10
  • show me your response array . Commented Mar 26, 2013 at 5:10
  • 1
    Please check null. I think there is some null in your array. Thats why its crashing Commented Mar 26, 2013 at 5:11
  • 1
    try to print your array. check if it contains value or not? Commented Mar 26, 2013 at 5:15
  • 1
    @Praveen are you able to print the values before setText Commented Mar 26, 2013 at 5:32

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.