开发者

How to call numberOfRowsInSection and cellForRowAtIndexPath alternatively

开发者 https://www.devze.com 2023-03-28 04:29 出处:网络
I am working on an application in which data is displaying from back end to tableview. The problem is, when table is loading, it gets all the data but displays only the last entry of database in each

I am working on an application in which data is displaying from back end to tableview. The problem is, when table is loading, it gets all the data but displays only the last entry of database in each row. suppose last section has 3 rows then it displays only3 that 3 row data in each section.Even if in some section rows are 9 then the rest of rows are displaying blank.

I found it through break points that table first takes all the data from database and then read cellForRowAtIndexPath, As in my database last table has 3 rows, it displays the data of 3 rows only by keeping rest of rows blank.

Here is the coe of my numberOfRowsInSection and cellForRowAtIndexPath

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.

    if(requestType == 2) 
    {       
        if (self.uniqueCityCount > 0) 
        {
            NSString *strCity = [self.arrayCityNames objectAtIndex:section];            //@"EventData" :@"EventCity"
            NSPredicate *predicateSettings =  [NSPredicate predicateWithFormat:@"(EventCity = %@ )",strCity]; 
            if ([self.arrayEventDescription count]>0)开发者_如何学编程
            {
                [self.arrayEventDescription removeAllObjects];
            }
            self.arrayEventDescription = [CoreDataAPIMethods searchObjectsInContext:@"EventData" :predicateSettings :@"EventCity" :YES :self.managedObjectContext];
            return [self.arrayEventDescription count];  
        }
        /*
         if (section == 0)
         {
         NSString *strCity = [self.arrayCityNames objectAtIndex:section];           //@"EventData" :@"EventCity"
         NSPredicate *predicateSettings =  [NSPredicate predicateWithFormat:@"(EventCity = %@ )",strCity]; 
         self.arrayEventDescription = [CoreDataAPIMethods searchObjectsInContext:@"EventData" :predicateSettings :@"EventCity" :YES :self.managedObjectContext];
         return [self.arrayEventDescription count]; 
         }
         else if (section == 1)
         {

         }*/
    }       
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
NSString *CellIdentifier = [@"" stringByAppendingFormat:@"Cell%d",indexPath.row];      
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

cell.backgroundColor = [UIColor clearColor];

if(requestType == 2)
{   
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        //  for (int j = 0 ; j < [self.arrayEventDescription count]; j++)

        //  for(int j=0; j<[tableView numberOfSections]; j++)

        //{

        NSLog(@"at section %d",indexPath.section);
        NSLog(@"at row %d",indexPath.row);
        NSLog(@"array count %d",[self.arrayEventDescription count]);

        if (indexPath.row < [self.arrayEventDescription count]) 
        {


            EventData *data = [self.arrayEventDescription objectAtIndex:indexPath.row];
            //EventData *data = [self.arrayEventDescription objectAtIndex:j];

            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.backgroundColor=[UIColor clearColor];      


            //  UIView *viewDescription = [[UIView alloc]initWithFrame:CGRectMake(00, 00, 480, 35)];

            //////////////////////    Labels for description of city events from database  ////////////////////////////             

            UILabel *lblEvent = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 150, 30)];
            lblEvent.font = [UIFont systemFontOfSize:12];
            lblEvent.backgroundColor = [UIColor clearColor];

            UILabel *lblEventAtDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 150, 30)];
            lblEventAtDate.font = [UIFont systemFontOfSize:12];
            lblEventAtDate.backgroundColor = [UIColor clearColor];

            UILabel *lblEventAtSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 15, 150, 30)];
            lblEventAtSchool.font = [UIFont systemFontOfSize:12];
            lblEventAtSchool.backgroundColor = [UIColor clearColor];

            [cell.contentView addSubview:lblEvent];
            [cell.contentView addSubview:lblEventAtDate];
            [cell.contentView addSubview:lblEventAtSchool];


            lblEvent.text = data.EventName;
            //  lblEventAtDate.text = data.EventDate;
            lblEventAtSchool.text = data.EventPlace;
        }



        //} 
    }
}


// Configure the cell...

return cell;
}


As a first thing, I moved everything out of cell == nil block, as reuse may not happen. See if this solves your problem.

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

{    

NSString *CellIdentifier = [@"" stringByAppendingFormat:@"Cell%d",indexPath.row];      
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor clearColor];

if(requestType == 2)
{   
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        //  for (int j = 0 ; j < [self.arrayEventDescription count]; j++)

        //  for(int j=0; j<[tableView numberOfSections]; j++)

        //{

        NSLog(@"at section %d",indexPath.section);
        NSLog(@"at row %d",indexPath.row);
        NSLog(@"array count %d",[self.arrayEventDescription count]);





        //} 
    }

    if (indexPath.row < [self.arrayEventDescription count]) 
    {


        EventData *data = [self.arrayEventDescription objectAtIndex:indexPath.row];
        //EventData *data = [self.arrayEventDescription objectAtIndex:j];

        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.backgroundColor=[UIColor clearColor];      


        //  UIView *viewDescription = [[UIView alloc]initWithFrame:CGRectMake(00, 00, 480, 35)];

        //////////////////////    Labels for description of city events from database  ////////////////////////////             

        UILabel *lblEvent = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 150, 30)];
        lblEvent.font = [UIFont systemFontOfSize:12];
        lblEvent.backgroundColor = [UIColor clearColor];

        UILabel *lblEventAtDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 150, 30)];
        lblEventAtDate.font = [UIFont systemFontOfSize:12];
        lblEventAtDate.backgroundColor = [UIColor clearColor];

        UILabel *lblEventAtSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 15, 150, 30)];
        lblEventAtSchool.font = [UIFont systemFontOfSize:12];
        lblEventAtSchool.backgroundColor = [UIColor clearColor];

        [cell.contentView addSubview:lblEvent];
        [cell.contentView addSubview:lblEventAtDate];
        [cell.contentView addSubview:lblEventAtSchool];


        lblEvent.text = data.EventName;
        //  lblEventAtDate.text = data.EventDate;
        lblEventAtSchool.text = data.EventPlace;
    }
}


// Configure the cell...

return cell;

}


Can you tell me what is the request type and when it will be called(a set of possible values).

Quick Solution: You are checking the condition of request type first if(requestType == 2) { and then you are loading the cell. This delegate method will be called on loading the tableview, but requesttype is 2. So for indexpath.row=0 & 1, it wont go inside to load the cell and it loads only for requesttype=2. So it is loading third row.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号