2009-10-22 63 views
2

我有一個名爲Person的類,在這個類中是屬性PersonName(等等)。Objective-C與NSMutableArray和NSTableView問題

MutableArray MyUserInfoArr包含許多Person對象。

我想列出TableView單元格中的每個PersonName?我該怎麼做呢? 在此先感謝。

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

    static NSString *CellIdentifier = @"Cell"; 
    Person *myPerson;  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSString *cellValue = [myUserInfoArr objectAtIndex:indexPath.row]; 
    cell.text = cellValue; 

if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
     Person *myPerson = [myUserInfoArr objectAtIndex:indexPath.row]; 
     cell.textLabel.text = myPerson.DisplayName; 
     NSLog(@"abc %i",indexPath.section); 
} 

這是我的代碼,它跑了,但它只是在myUserInfoArr返回的最後一個對象人物的屬性Person.DisplayName。如何解決它?

回答

1
Person *person = [myUserInfoArr objectAtIndex:indexPath.row]; 
cell.textLabel.text = person.PersonName; 

您的代碼不起作用的原因是: text屬性需要是NSString *,而不是Person *

+0

但邏輯中的[myUserInfoArr objectAtIndex:indexPath.row]不會返回NSString,因爲myUserInfoArr包含Person類的許多對象。 – ThyPhuong 2009-10-22 10:24:02

2

最簡單的方法是隻設置與文本:

Person *person = [MyUserInfoArr objectAtIndex:[indexPath indexAtPosition:1]]; 
cell.textLabel.text = person.PersonName; 

indexPath包含兩個指標,第一個是部分指數,第二是部分中的項目的索引。所以我假設你的情況你有一個單獨的部分,這就是爲什麼沒有任何與indexAtPosition:0。

您還可以設置表的數據源的方法,這些告訴你的表視圖應該有多少段/行顯示:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [MyUserInfoArr count]; 
} 
+0

感謝您的回覆,它顯示的行數在我的TableView但在該行: cell.textLabel.text = [MyUserInfoArr objectAtIndex:[indexPath indexAtPosition:1]。PERSONNAME 有一個錯誤:請求更多的成員'PersonName'不是結構或聯盟。那意味着什麼? – ThyPhuong 2009-10-22 09:23:23

+0

@ThyPhuong:錯誤可能意味着返回的東西不是一個人*。我認爲問題在於鏈接的語法不被支持。我已經編輯了我的上面的答案,現在就試試。 – Prody 2009-10-22 13:51:20