【问题标题】:How to call numberOfRowsInSection and cellForRowAtIndexPath alternatively如何交替调用 numberOfRowsInSection 和 cellForRowAtIndexPath
【发布时间】:2011-08-16 10:08:25
【问题描述】:

我正在开发一个数据从后端显示到表格视图的应用程序。 问题是,当表加载时,它获取所有数据,但每行只显示数据库的最后一个条目。假设最后一节有 3 行,那么它在每个节中只显示 3 行数据。即使在某些节中的行是 9 行,其余行也显示为空白。

我通过断点发现表首先从数据库中获取所有数据然后读取cellForRowAtIndexPath,因为在我的数据库中最后一个表有3行,它仅通过将其余行保持空白来显示3行数据。

这是我的 numberOfRowsInSection 和 cellForRowAtIndexPath 的 coe

- (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;
}

【问题讨论】:

    标签: objective-c uitableview ios4 tableview


    【解决方案1】:

    首先,我将所有内容移出 cell == nil 块,因为可能不会发生重用。看看这是否能解决您的问题。

    - (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;
    

    }

    【讨论】:

    • 数据以与之前相同的方式显示,但这次,相同的数据再次与广告重叠,因此由于重叠,字母以粗体显示。
    • 我可以看到您的 CGRectMake 在同一屏幕坐标处重复创建控件。您可以考虑为此使用自定义表格单元格吗?请参考:e-string.com/content/custom-uitableviewcells-interface-builder
    【解决方案2】:

    你能告诉我什么是请求类型,什么时候会被调用(一组可能的值)。

    快速解决方案:您首先检查请求类型的条件if(requestType == 2) {,然后您正在加载单元格。这个委托方法会在加载tableview时被调用,但是requesttype是2。所以对于indexpath.row=0&1,它不会去里面加载单元格,它只加载requesttype=2。所以它正在加载第三行。

    【讨论】:

    • RequestType 是一个整数。实际上我有 4 个子视图附加到 xib 视图。这是我的 n 的第二个视图,因此请求类型 2 将在显示第二个视图时调用,其中我有上面讨论的表格视图。
    • RequestType 只是一个整数,我已经定义了部分的数量,如果 RequestType 为 2,那么部分的数量是数组计数。所以我认为 RequestType 不会产生任何问题。我使用 ReuestType 来区分四个视图,因为每个视图都包含一个表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多