3

I am new to iPhone Development. My app is sending URL request to retrieve data from server by using NSURLConnection. I'm getting data in JSON format and I'm storing it in NSMutableArray as array of dictionaries. So I have an array of dictionaries. I need to display specific data in UITableViewCell.

My code is Here...

-(IBAction)ButtonTapped:(id)sender
{
     NSString *strURL=[NSString stringWithFormat:@"URl Name here"];
     NSURL *url=[NSURL URLWithString:strURL];
     self.request=[NSURLRequest requestWithURL:url]
     self.nsCon=[[NSURLConnection alloc] initWithRequest:request delegate:self];

     if(self.nsCon)
     {
        self.receivedData=[[NSMutableData alloc] init];
     }
     else
     {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error",@"") message:NSLocalizedString(@"Not Connected Other View !!",@"") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil];
        [alert show];
        [alert release];
     }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // receivedData is an instance variable declared elsewhere.
    [receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to receivedData.
    // receivedData is an instance variable declared elsewhere.
    [receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    // inform the user

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error",@"") message:NSLocalizedString(@"Connection failed !!",@"") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil];
    [alert show];
    [alert release];

}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *responseString = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];     
    self.theDictionary= [[NSMutableDictionary alloc] init];
    self.theDictionary = [responseString JSONValue];

    self.arrofDictionary=[[NSMutableArray alloc] init];
     [self.arrofDictionary addObject:self.theDictionary];

    NSLog(@"data length - %@ ",[self.theDictionary objectForKey:@"today"]);

    // release the connection, and the data object
    [connection release];
    [receivedData release];

    self.tblList=[[UITableView alloc] initWithFrame:CGRectMake(0,80,320,370) style:UITableViewStyleGrouped];
    self.tblList.delegate=self;
    self.tblList.dataSource=self;
    [self.view addSubview:self.tblList];

}

// data display in consol

(
        {
        "date_time" = "2012-08-31 09:30:25";
        id = 99;
        language = en;
        name = Tt;
        score = 656;
    },
        {
        "date_time" = "2012-08-31 10:08:52";
        id = 103;
        language = en;
        name = Fg;
        score = 567;
    },
        {
        "date_time" = "2012-08-31 08:22:38";
        id = 87;
        language = en;
        name = Eree;
        score = 565;
    },
)

Then how can I display name and score on UITableViewCell ??

3
  • Don't store data in your table cells, that completely breaks MVC. Commented Aug 31, 2012 at 10:21
  • Read this: developer.apple.com/library/ios/#DOCUMENTATION/iPhone/… Commented Aug 31, 2012 at 10:29
  • Keep MCV, store all the data in One Modal class and later user use that Modal class object to show data on the cell.Do not store on the Cell. Commented Aug 31, 2012 at 10:32

1 Answer 1

1

You displayed data is in array of dictionary objects(yourArray). You use following code.

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

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }
     cell.textLabel.text = [[yourArray objectAtIndex:indexPath.row]  objectForKey:@"name"];
    cell.detailTextLabel.text = [[yourArray objectAtIndex:indexPath.row]  objectForKey:@"score"];



    return cell;
}

I think it will be helpful to you.

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

7 Comments

How have you created table view? Which array is based on?
i create tableview when Button is tapped( -(IBAction)ButtonTapped:(id)sender first method is called ).. and self.arrofDictionary array used
self.arrofDictionary is your resulting array i mean displayed array using console. Am i right?
Is tableview displayed and how many rows it contained? The number of rows are equal to the number of objects in self.arrofDictionary array. Am i right?
yes tableview is displayed.. but only one row is display and it is also blank...,yes row is equal to [self.arrofDictionary count];
|

Your Answer

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