Ios4 uitableview中的for循环

Ios4 uitableview中的for循环,ios4,core-data,for-loop,uitableview,Ios4,Core Data,For Loop,Uitableview,我正在开发一个应用程序,其中显示来自后端的数据 现在的问题是,当我用for循环填充我的表时,循环的每个单元格都在通过每次调用填充数据。假设数据库中有9个条目,我希望它们按行显示,但所有9行都是由9个相互重叠的条目填充的 由于我是iphone开发新手,请帮助我 这是我的一段代码,我正在尝试,但没有成功 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // R

我正在开发一个应用程序,其中显示来自后端的数据

现在的问题是,当我用for循环填充我的表时,循环的每个单元格都在通过每次调用填充数据。假设数据库中有9个条目,我希望它们按行显示,但所有9行都是由9个相互重叠的条目填充的

由于我是iphone开发新手,请帮助我

这是我的一段代码,我正在尝试,但没有成功

- (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];  
        }
}       
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImageView *imgEventLabel = [[UIImageView alloc]initWithFrame:CGRectMake(0, 5, 480, 22)];
imgEventLabel.image = [UIImage imageNamed:@"city_date_place.png"];


UILabel *lblCity = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 200, 22)];
lblCity.font = [UIFont systemFontOfSize:14];
lblCity.backgroundColor = [UIColor clearColor];
lblCity.tag = 301;
//lblCity.backgroundColor = [UIColor redColor];

UILabel *lblDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 200, 22)]; 
lblDate.font = [UIFont systemFontOfSize:14];
lblDate.backgroundColor = [UIColor clearColor];
lblDate.tag = 302;
//lblDate.backgroundColor = [UIColor redColor];

UILabel *lblSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 00, 400, 22)];
lblSchool.font = [UIFont systemFontOfSize:14];
lblSchool.backgroundColor = [UIColor clearColor];
lblSchool.tag = 303;

//lblSchool.backgroundColor = [UIColor redColor];
[imgEventLabel addSubview:lblCity];
[imgEventLabel addSubview:lblDate];
[imgEventLabel addSubview:lblSchool];

return imgEventLabel;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 22;
}

// Customize the appearance of table view cells.
- (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++)
            {

                NSLog(@"%d",indexPath.row);
                //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;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
//返回节中的行数。
if(requestType==2)
{       
if(self.uniqueCityCount>0)
{
NSString*strCity=[self.arrayCityNames objectAtIndex:section];/@“EventData”:@“EventCity”
NSPredicate*predicateSettings=[NSPredicate predicateWithFormat:@“(EventCity=%@)”,strCity];
如果([self.arrayeventddescription count]>0)
{
[self.arrayEventDescription removeAllObjects];
}
self.arrayeventddescription=[CoreDataAPIMethods searchObjectsInContext:@“EventData”:谓词设置:@“EventCity”:是:self.managedObjectContext];
返回[self.arrayeventddescription count];
}
}       
返回0;
}
-(UIView*)表格视图:(UITableView*)表格视图用于标题部分:(NSInteger)部分
{
UIImageView*imgEventLabel=[[UIImageView alloc]initWithFrame:CGRectMake(0,5480,22)];
imgEventLabel.image=[UIImage ImageName:@“city\u date\u place.png”];
UILabel*lblCity=[[UILabel alloc]initWithFrame:CGRectMake(15,00,200,22)];
lblCity.font=[UIFont systemFontOfSize:14];
lblCity.backgroundColor=[UIColor clearColor];
lblCity.tag=301;
//lblCity.backgroundColor=[UIColor redColor];
UILabel*lblDate=[[UILabel alloc]initWithFrame:CGRectMake(200,00,200,22)];
lblDate.font=[UIFont systemFontOfSize:14];
lblDate.backgroundColor=[UIColor clearColor];
lblDate.tag=302;
//lblDate.backgroundColor=[UIColor redColor];
UILabel*lblSchool=[[UILabel alloc]initWithFrame:CGRectMake(350,00,400,22)];
lblSchool.font=[UIFont systemFontOfSize:14];
lblSchool.backgroundColor=[UIColor clearColor];
lblSchool.tag=303;
//lblSchool.backgroundColor=[UIColor redColor];
[imgEventLabel addSubview:lblCity];
[imgEventLabel addSubview:lblDate];
[imgEventLabel addSubview:lblSchool];
返回imgEventLabel;
}
-(CGFloat)表格视图:(UITableView*)表格视图头部高度部分:(NSInteger)部分
{
返回22;
}
//自定义表格视图单元格的外观。
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{    
NSString*CellIdentifier=[@“stringByAppendingFormat:@“单元格%d”,indexath.row];
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.backgroundColor=[UIColor clearColor];
if(requestType==2)
{   
如果(单元格==nil)
{
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier]自动释放];
对于(int j=0;j<[self.arrayeventddescription count];j++)
{
NSLog(@“%d”,indexath.row);
//EventData*data=[self.arrayeventddescription 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)];
//////////////////////用于描述数据库中城市事件的标签
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*LBLeventSchool=[[UILabel alloc]initWithFrame:CGRectMake(350,15,150,30)];
lbleventtschool.font=[UIFont systemFontOfSize:12];
lbleventtschool.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:lblEvent];
[cell.contentView addSubview:lblEventAtDate];
[cell.contentView addSubview:lbleventtschool];
lblEvent.text=data.EventName;
//lblEventAtDate.text=data.EventDate;
lbleventtschool.text=data.EventPlace;
}   
}
}
//配置单元格。。。
返回单元;
}

不要在阵列中循环。只需获取所请求行的元素(即
indexPath.row

cellforrowatinexpath:
将被调用的次数与您在
numberofrowsin部分:
中指定的次数相同


每次调用一个单元格。

跳过for循环,使用[self.arrayeventddescriptoin-objectAtIndes:indexPath.row];您将收到9个对cellForRowAtIndexPath的调用。我也尝试了dat,但在使用indexPath时。row应用程序正在崩溃!!!由于未捕获的异常“NSRangeException”而终止应用程序,原因:“***-[NSMutableArray objectAtIndex:]:索引3超出边界[0..2]”实际上应用程序在加载最后一行时崩溃,但我不知道为什么。因此,您的数组只包含3个对象?但你至少接到4个电话到cellForRowAtIndexPath?这很奇怪。