NsmutableArray仅显示在所有索引路径上添加的最后一个对象

时间:2014-02-17 07:49:19

标签: ios nsmutablearray

我陷入了困境。我想显示从数组到tableView的虚拟数据。但我总是只在所有索引路径上获得最后一个对象。我不知道为什么它会被最后一个对象压倒

-(NSMutableArray *)clubTypes
{
    NSMutableArray *clubTypeDataArray=[[NSMutableArray alloc]init];
    ClubTypeDC *clubTypes = [[ClubTypeDC alloc] init];
//    slideMenu_dataArray = [[NSMutableArray alloc] init];
    clubTypes.clubTypeID = 1;
    clubTypes.clubTypeTitle = @"Hot tonight";
    clubTypes.clubTypeImage = @"icon_hot_tonight.png";
    clubTypes.clubTypeSelectedImage = @"icon_hot_tonight_touch.png";
    [clubTypeDataArray addObject:clubTypes];

    clubTypes.clubTypeID = 2;
    clubTypes.clubTypeTitle = @"Lounges";
    clubTypes.clubTypeImage = @"icon_lounges.png";
    clubTypes.clubTypeSelectedImage = @"icon_lounges_touch.png";
    [clubTypeDataArray addObject:clubTypes];

    clubTypes.clubTypeID = 3;
    clubTypes.clubTypeTitle = @"Pubs/Sports bars";
    clubTypes.clubTypeImage = @"icon_pubs.png";
    clubTypes.clubTypeSelectedImage = @"icon_pubs_touch.png";
    [clubTypeDataArray addObject:clubTypes];

    clubTypes.clubTypeID = 4;
    clubTypes.clubTypeTitle = @"Shesha bars";
    clubTypes.clubTypeImage = @"icon_shisha_bars.png";
    clubTypes.clubTypeSelectedImage = @"icon_shisha_bars_touch.png";
    [clubTypeDataArray addObject:clubTypes];

    clubTypes.clubTypeID = 5;
    clubTypes.clubTypeTitle = @"Ladies night";
    clubTypes.clubTypeImage = @"icon_ladies_night.png";
    clubTypes.clubTypeSelectedImage = @"icon_ladies_night_touch.png";
    [clubTypeDataArray addObject:clubTypes];

    clubTypes.clubTypeID = 6;
    clubTypes.clubTypeTitle = @"Music festivals";
    clubTypes.clubTypeImage = @"icon_music_festivals.png";
    clubTypes.clubTypeSelectedImage = @"icon_music_festivals_touch.png";
    [clubTypeDataArray addObject:clubTypes];

    clubTypes.clubTypeID = 7;
    clubTypes.clubTypeTitle = @"Boat parties";
    clubTypes.clubTypeImage = @"icon_boat_parties.png";
    clubTypes.clubTypeSelectedImage = @"icon_boat_parties_touch.png";
    [clubTypeDataArray addObject:clubTypes];

    clubTypes.clubTypeID = 8;
    clubTypes.clubTypeTitle = @"Evening brunches";
    clubTypes.clubTypeImage = @"icon_evening_brunches.png";
    clubTypes.clubTypeSelectedImage = @"icon_evening_brunches_touch.png";
    [clubTypeDataArray addObject:clubTypes];

    clubTypes.clubTypeID = 9;
    clubTypes.clubTypeTitle = @"Beach/ Description";
    clubTypes.clubTypeImage = @"icon_beach_parties.png";
    clubTypes.clubTypeSelectedImage = @"icon_beach_parties_touch.png";
    [clubTypeDataArray addObject:clubTypes];

    clubTypes.clubTypeID = 10;
    clubTypes.clubTypeTitle = @"Configuration";
    clubTypes.clubTypeImage = @"icon_configuration.png";
    clubTypes.clubTypeSelectedImage = @"icon_configuration_touch.png";
    [clubTypeDataArray addObject:clubTypes];

    clubTypes.clubTypeID = 11;
    clubTypes.clubTypeTitle = @"About Unighted";
    clubTypes.clubTypeImage = @"icon_about.png";
    clubTypes.clubTypeSelectedImage = @"icon_about_touch.png";
    [clubTypeDataArray addObject:clubTypes];

//    [clubTypeDataArray addObject:clubTypes];

    return clubTypeDataArray;

}

请帮我解决问题。

3 个答案:

答案 0 :(得分:2)

因为您没有创建新对象,所以您只需反复添加和修改同一个对象。

请改为:

-(NSMutableArray *)clubTypes
{
    NSMutableArray *clubTypeDataArray=[[NSMutableArray alloc]init];
    ClubTypeDC *clubTypes = [[ClubTypeDC alloc] init];
//    slideMenu_dataArray = [[NSMutableArray alloc] init];
    clubTypes.clubTypeID = 1;
    clubTypes.clubTypeTitle = @"Hot tonight";
    clubTypes.clubTypeImage = @"icon_hot_tonight.png";
    clubTypes.clubTypeSelectedImage = @"icon_hot_tonight_touch.png";
    [clubTypeDataArray addObject:clubTypes];

    clubTypes = [[ClubTypeDC alloc] init];
    clubTypes.clubTypeID = 2;
    clubTypes.clubTypeTitle = @"Lounges";
    clubTypes.clubTypeImage = @"icon_lounges.png";
    clubTypes.clubTypeSelectedImage = @"icon_lounges_touch.png";
    [clubTypeDataArray addObject:clubTypes];

    clubTypes = [[ClubTypeDC alloc] init];
    clubTypes.clubTypeID = 3;
    clubTypes.clubTypeTitle = @"Pubs/Sports bars";
    clubTypes.clubTypeImage = @"icon_pubs.png";
    clubTypes.clubTypeSelectedImage = @"icon_pubs_touch.png";
    [clubTypeDataArray addObject:clubTypes];

    ...
}

答案 1 :(得分:2)

请注意,每次向数组添加对象时,它都是同一个对象。

每次配置时都必须创建新对象,因此代码为:

ClubTypeDC *clubTypes = [[ClubTypeDC alloc] init];

应该预先添加所有配置片段。

答案 2 :(得分:1)

您需要每次为新对象实例化ClubTypeDC,否则您将编辑同一对象引用的属性。这就是为什么数组中的所有元素都会使用上次更新的数据进行更新

-(NSMutableArray *)clubTypes
{
    NSMutableArray *clubTypeDataArray=[[NSMutableArray alloc]init];
    ClubTypeDC *clubTypes = [[ClubTypeDC alloc] init];
    clubTypes.clubTypeID = 1;
    clubTypes.clubTypeTitle = @"Hot tonight";
    clubTypes.clubTypeImage = @"icon_hot_tonight.png";
    clubTypes.clubTypeSelectedImage = @"icon_hot_tonight_touch.png";
    [clubTypeDataArray addObject:clubTypes];

    clubTypes = [[ClubTypeDC alloc] init];
    clubTypes.clubTypeID = 2;
    clubTypes.clubTypeTitle = @"Lounges";
    clubTypes.clubTypeImage = @"icon_lounges.png";
    clubTypes.clubTypeSelectedImage = @"icon_lounges_touch.png";
    [clubTypeDataArray addObject:clubTypes];

    .....
 }