2013-03-05 73 views
0

我正在尋找一種方法來填充表視圖與兩個單獨的(但並行)數組中的圖像和文本。我一直試圖從兩個使用所有可能的內容實例化的可變數組開始,檢查模型對象的內容,刪除「空白」條目,並將剩下的內容複製到填充表視圖單元格的表視圖屬性中。如何從兩個數組中填充表格視圖內容?

(該person對象從中profileList越來越其初始值是在正在傳遞的模型對象。)

NSMutableArray *profileList = [@[person.facebook, person.twitter, person.pinterest, person.linkedIn, person.youTube, person.googlePlus, person.flickr] mutableCopy]; 
    NSMutableArray *buttonList = [@[@"btn-Facebook.png", @"btn-Twittter.png", @"btn-Pinterest.png", @"btn-LinkedIn.png", @"btn-YouTube.png", @"btn-Google+.png", @"btn-Flickr.png"] mutableCopy]; 
    NSMutableArray *indicesToRemove = [@[] mutableCopy]; 

    // for (NSString *index in profileList) { 
    // 
    // } 
    int i = 0; 
    for (NSString *indexContents in profileList) { 
     if ([indexContents isEqual: @""]) { 
     // [indicesToRemove addObject:[NSString stringWithFormat:@"%d", i]]; 
      [indicesToRemove addObject:indexContents]; 
     } 
     i++; 
    } 
    for (NSString *count in indicesToRemove) { 
     // [profileList removeObjectAtIndex:[count integerValue]]; 
     [profileList removeObject:count]; 
     // [buttonList removeObjectAtIndex:[count integerValue]]; 
    } 

    self.socialMediaProfilesList = (NSArray *)profileList; 
    self.socialMediaButtonsList = (NSArray *)buttonList; 

這適用於profileList但不能用於buttonList。 (由於後來,在tableView:cellForRowAtIndexPath:cell.textLabel.text套不錯,但cell.imageView.image拋出異常。)

正如你或許可以從註釋掉的線條訴說,我試圖重構這個方法來跟蹤指數,而不是內容,而是與我甚至無法加載表格。

看來我似乎正在全力以赴,但我無法想出更好的方法。有任何想法嗎?

+0

我應該補充一點:我不是在尋找任何人爲我編寫解決方案,只是幫助我找到如何解決(或開始自己的新方向)代碼。 – ele 2013-03-05 22:43:10

回答

1

好的,指針:)

  • 而不是使用快速枚舉,使用普通的for循環(對於(INT I = 0;我< COUNT;我++))
  • 商店的所有索引中一個NSMutableIndexSet
  • 使用removeObjectsAtIndexes:來清除所有對象,通過調用它兩個陣列上

編輯:或者,替代ly,顛倒for-loop的順序(int = COUNT-1; i> = 0;我 - )並直接從陣列中刪除對象。

+0

你知道嗎,我從來不知道或看着'NSIndexSet'類或它們的用途。但是,通過數組反向工作是完美的,簡潔的,我從來沒有想到它。謝謝您的幫助! – ele 2013-03-06 15:46:23