Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cell for row at indexpath only called once

I am parsing a json data and in that one of the field is an array if it has a content.But if it has no content it is simply a string "No result." I am using the following code to parse it :

-(void)viewWillAppear:(BOOL)animated


{
[super viewWillAppear:YES];
//URL is called.
if ([status isEqualToString:@"success"])

{
                if ([[[json objectForKey:@"Items"] objectForKey:@"item_list"] isKindOfClass:[NSString class]])



                {
                    _Label.text = [NSString stringWithFormat:@"0"];
                    ItemsArray = [[NSMutableArray alloc]init];
                }
                else
                {
                    ItemsArray = [[json objectForKey:@"Items"] objectForKey:@"item_list"];
                    NSLog(@"%d",[ItemsArray count]);
                    float Amount = 0;
                    NSLog(@"%d",[ItemsArray count]);
                    if ([ItemsArray count]>0)
                    {
                        for (int i=0; i<[ItemsArray count]; i++)
                        {
                            NSMutableDictionary * temp3 = [ItemsArray objectAtIndex: i];
                            NSString * x = [temp3 objectForKey:@"final"];
                            Amount = Amount + [x floatValue];
                        }
                        Label.text = [NSString stringWithFormat:@"%.2f",BidAmount];
                    }
                    else
                    {
                        Label.text = [NSString stringWithFormat:@"0"];
                    }
                }



                [TableViewOutlet reloadData];

}

This works fine.

When the field returns string i face no problem.But when the field is an array,I am stuck. After running the project for the first time it works fine even if the array count is more than 0.But from second time onwards the Cellforrowatindexpath method is not called even when array count is more than 0.... These are my codes:

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"%d",[ItemsArray count]);
return  [ItemsArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
static NSString *CellIdentifier = @"TableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (cell==nil)
{
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.025];
NSDictionary *temp = [ItemsArray objectAtIndex:indexPath.row];

UILabel *ItemName = (UILabel *)[cell viewWithTag:1000];
ItemName.text = [temp objectForKey:@"title"];

UILabel *Cost = (UILabel *)[cell viewWithTag:1001];
Cost.text = [temp objectForKey:@"final"];

return cell;

}

Someone please help me

like image 731
Joe Mathews Avatar asked Nov 14 '14 07:11

Joe Mathews


People also ask

What does indexPath row return?

So to start with the indexPath. row will be 0. Then it will be 1, then 2, then 3 and so on. You do this so that you can get the correct string from the array each time.

How do you get the indexPath row when a button in a cell is tapped?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

How can we use a reusable cell in UITableView?

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.


1 Answers

I think the issue is that you have not retained your ItemArray.

NSArray* array = [[json objectForKey:@"Items"] objectForKey:@"item_list"];
ItemsArray = [NSMutableArray alloc]]initWithArray:array];

Use this code snippet..hope it will work.

like image 133
Meenakshi Avatar answered Nov 09 '22 16:11

Meenakshi