Ios 如何按特定顺序将项目添加到UITableView?

Ios 如何按特定顺序将项目添加到UITableView?,ios,uitableview,Ios,Uitableview,我正在制作一个应用程序,在UITableView中有一个项目列表,该列表从数组中获取数据。这些项被加载到列表中很好,但是它们的顺序与我在loadAnnotations方法中的顺序不同(请继续阅读) 下面是加载注释的代码(这是我在列表中显示的内容),以及将UITable中单元格的文本设置为注释名称的代码 所有东西都很好地加载到我的列表中,但顺序是随机的。有什么想法吗 -(void) loadOutAnnotations { CLLocationCoordinate2D workingCo

我正在制作一个应用程序,在UITableView中有一个项目列表,该列表从数组中获取数据。这些项被加载到列表中很好,但是它们的顺序与我在loadAnnotations方法中的顺序不同(请继续阅读)

下面是加载注释的代码(这是我在列表中显示的内容),以及将UITable中单元格的文本设置为注释名称的代码

所有东西都很好地加载到我的列表中,但顺序是随机的。有什么想法吗

-(void) loadOutAnnotations
{

    CLLocationCoordinate2D workingCoordinate;

    //***********HOTELS**************************

    workingCoordinate.latitude = 40.763856;
    workingCoordinate.longitude = -73.973034;
    iCodeBlogAnnotation *hotel1 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel1 setTitle:@"Hotel 1"];
    [hotel1 setSubtitle:@"This is Hotel #1"];
    [hotel1 setAnnotationType:iCodeBlogAnnotationTypeHotels];

    [mapView addAnnotation:hotel1];


    workingCoordinate.latitude = 50.763856;
    workingCoordinate.longitude = -83.973034;
    iCodeBlogAnnotation *hotel2 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel2 setTitle:@"Hotel 2"];
    [hotel2 setSubtitle:@"This is Hotel #2"];
    [hotel2 setAnnotationType:iCodeBlogAnnotationTypeHotels];

    [mapView addAnnotation:hotel2];

    workingCoordinate.latitude = 60.763856;
    workingCoordinate.longitude = -93.973034;
    iCodeBlogAnnotation *hotel3 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel3 setTitle:@"Hotel 3"];
    [hotel3 setSubtitle:@"This is Hotel #3"];
    [hotel3 setAnnotationType:iCodeBlogAnnotationTypeHotels];

    [mapView addAnnotation:hotel3];

    workingCoordinate.latitude = 10.763856;
    workingCoordinate.longitude = -23.973034;
    iCodeBlogAnnotation *hotel4 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel4 setTitle:@"Hotel 4"];
    [hotel4 setSubtitle:@"This is Hotel #4"];
    [hotel4 setAnnotationType:iCodeBlogAnnotationTypeHotels];

    [mapView addAnnotation:hotel4];

    workingCoordinate.latitude = 30.763856;
    workingCoordinate.longitude = -43.973034;
    iCodeBlogAnnotation *hotel5 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel5 setTitle:@"Hotel 5"];
    [hotel5 setSubtitle:@"This is Hotel #5"];
    [hotel5 setAnnotationType:iCodeBlogAnnotationTypeHotels];

    [mapView addAnnotation:hotel5];

    // *****************AERIALS*****************************

    workingCoordinate.latitude = 25.763856;
    workingCoordinate.longitude = -15.973034;
    iCodeBlogAnnotation *aerial1 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [aerial1 setTitle:@"Aerial 1"];
    [aerial1 setSubtitle:@"This is Aerial #1"];
    [aerial1 setAnnotationType:iCodeBlogAnnotationTypeAerialShots];

    [mapView addAnnotation:aerial1];


    workingCoordinate.latitude = 35.763856;
    workingCoordinate.longitude = -25.973034;
    iCodeBlogAnnotation *aerial2 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [aerial2 setTitle:@"Aerial 2"];
    [aerial2 setSubtitle:@"This is Aerial #2"];
    [aerial2 setAnnotationType:iCodeBlogAnnotationTypeAerialShots];

    [mapView addAnnotation:aerial2];

    workingCoordinate.latitude = 45.763856;
    workingCoordinate.longitude = -35.973034;
    iCodeBlogAnnotation *aerial3 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [aerial3 setTitle:@"Aerial 3"];
    [aerial3 setSubtitle:@"This is Aerial #3"];
    [aerial3 setAnnotationType:iCodeBlogAnnotationTypeAerialShots];

    [mapView addAnnotation:aerial3];

    workingCoordinate.latitude = 55.763856;
    workingCoordinate.longitude = -45.973034;
    iCodeBlogAnnotation *aerial4 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [aerial4 setTitle:@"Aerial 4"];
    [aerial4 setSubtitle:@"This is Aerial #4"];
    [aerial4 setAnnotationType:iCodeBlogAnnotationTypeAerialShots];

    [mapView addAnnotation:aerial4];

    workingCoordinate.latitude = 65.763856;
    workingCoordinate.longitude = -55.973034;
    iCodeBlogAnnotation *aerial5 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [aerial5 setTitle:@"Aerial 5"];
    [aerial5 setSubtitle:@"This is Aerial #5"];
    [aerial5 setAnnotationType:iCodeBlogAnnotationTypeAerialShots];

    [mapView addAnnotation:aerial5];


     // *****************STREETS*****************************

    workingCoordinate.latitude = 27.763856;
    workingCoordinate.longitude = -37.973034;
    iCodeBlogAnnotation *street1 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [street1 setTitle:@"Street 1"];
    [street1 setSubtitle:@"This is Street #1"];
    [street1 setAnnotationType:iCodeBlogAnnotationTypeStreets];

    [mapView addAnnotation:street1];

    workingCoordinate.latitude = 37.763856;
    workingCoordinate.longitude = -47.973034;
    iCodeBlogAnnotation *street2 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [street2 setTitle:@"Street 2"];
    [street2 setSubtitle:@"This is Street #2"];
    [street2 setAnnotationType:iCodeBlogAnnotationTypeStreets];

    [mapView addAnnotation:street2];

    workingCoordinate.latitude = 57.763856;
    workingCoordinate.longitude = -67.973034;
    iCodeBlogAnnotation *street3 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [street3 setTitle:@"Street 3"];
    [street3 setSubtitle:@"This is Street #3"];
    [street3 setAnnotationType:iCodeBlogAnnotationTypeStreets];

    [mapView addAnnotation:street3];

    workingCoordinate.latitude = 77.763856;
    workingCoordinate.longitude = -87.973034;
    iCodeBlogAnnotation *street4 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [street4 setTitle:@"Street 4"];
    [street4 setSubtitle:@"This is Street #4"];
    [street4 setAnnotationType:iCodeBlogAnnotationTypeStreets];

    [mapView addAnnotation:street4];

    workingCoordinate.latitude = 87.763856;
    workingCoordinate.longitude = -97.973034;
    iCodeBlogAnnotation *street5 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [street5 setTitle:@"Street 5"];
    [street5 setSubtitle:@"This is Street #5"];
    [street5 setAnnotationType:iCodeBlogAnnotationTypeStreets];

    [mapView addAnnotation:street5];
}





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

{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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


    }

    // here we are creating an array object BUT it is empty (it has no object in it)
    NSMutableArray *annotations = [[NSMutableArray alloc] init];



    if(indexPath.section == 0)
    {
        for(iCodeBlogAnnotation *annotation in [mapView annotations])
        {
            if([annotation annotationType] == iCodeBlogAnnotationTypeHotels)
            {
                 // here is the line where we actually add objects to the array
                [annotations addObject:annotation];
            }
        }


        if ([annotations count] > indexPath.row)
        {

        cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];

        }
        else
        {
        cell.textLabel.text = @"Empty";
        }


    }

    else if (indexPath.section == 1)
    {


        for(iCodeBlogAnnotation *annotation in [mapView annotations])
        {
            if([annotation annotationType] == iCodeBlogAnnotationTypeAerialShots)
            {
                [annotations addObject:annotation];
            }
        }

        if ([annotations count] > indexPath.row)
        {

            cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];

        }
        else
        {
            cell.textLabel.text = @"Empty";
        }



    }

    else if (indexPath.section == 2)
    {


        for(iCodeBlogAnnotation *annotation in [mapView annotations])
        {
            if([annotation annotationType] == iCodeBlogAnnotationTypeStreets)
            {
                [annotations addObject:annotation];
            }
        }

        if ([annotations count] > indexPath.row)
        {

            cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];

        }
        else
        {
            cell.textLabel.text = @"Empty";
        }


    }

    return cell;

}

我认为最好在cellForRowAtIndexPath之外创建数组,使用描述符或其他NSArray排序方法之一创建SortedArray

NSSortDescriptor *sorter = [NSSortDescriptor descriptorWithKey:@"aKey" ascending:YES];
NSArray *sortedArray = [[mapView annotations] sortedArrayUsingDescriptors:@[sorter]];

使用所需的任何键代替@“aKey”来获得所需的排序,然后使用sortedArray,以通常的方式填充表格。

为每个部分创建一个
地图视图:
HotelMap、天线地图、街道地图(或一组大小为#的分区,每个分区包含
MapView

将注释添加到适当的位置/部分。

对于数组实现
tableView:cellforrowatinexpath:
应该如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault)
                                      reuseIdentifier:CellIdentifier];
    }

    //_maps is a mutable array of MapView's
    MapView* map = (indexPath.section < [_maps count] ? _maps[indexPath.section] : nil);
    NSString* text = (map && indexPath.row < [[map annotations] count] ? [[[map annotations]  objectAtIndex:indexPath.row] title] : @"Empty");
    cell.textLabel.text = text;


    return cell;
}
-(UITableViewCell*)表视图:(UITableView*)表视图
cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil)
{
单元格=[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault)
reuseIdentifier:CellIdentifier];
}
//_maps是MapView的一个可变数组
MapView*map=(indexPath.section<[\u maps count]?\u maps[indexPath.section]:nil);
NSString*text=(映射和索引XPath.row<[[map annotations]计数]?[[map annotations]objectAtIndex:indexath.row]title]:@“空”);
cell.textlab.text=文本;
返回单元;
}