2009-10-22 4 views
2

저는 Person이라는 클래스가 있고이 클래스에는 PersonName 속성이 있습니다.목표 C - NSMutableArray 및 NSTableView 문제

MutableArray MyUserInfoArr에는 많은 Person 개체가 포함되어 있습니다.

각 PersonName을 TableView의 셀에 나열하고 싶습니까? 어떻게해야합니까? 미리 감사드립니다.


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); 
} 

- (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; 

내 코드, 그것은 실행하지만, 그냥 myUserInfoArr의 마지막 개체 사람의 ​​속성 Person.DisplayName을 반환합니다. 그것을 고치는 방법? 첫 번째는 섹션 내에서 항목의 인덱스, 섹션 인덱스,

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

indexPath 두 개의 인덱스를 포함

답변

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

코드가 작동하지 않는 이유 : text 속성은 이 아니라 NSString *이어야합니다. 답장을 보내

+0

하지만 myUserInfoArr에는 Person 클래스의 많은 객체가 포함되어 있기 때문에 로직의 [myUserInfoArr objectAtIndex : indexPath.row]는 NSString을 반환하지 않습니다. – ThyPhuong

2

가장 쉬운 방법은 단지와 텍스트를 설정하는 것입니다. 귀하의 경우 단일 섹션이 있다고 가정하기 때문에 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

+0

@ThyPhuong : 오류는 아마도 반환 된 것이 Person *이 아니란 것을 의미합니다. 문제는 체인 된 구문이 지원되지 않는다고 생각합니다. 위의 대답을 편집했습니다. 지금 시도하십시오. – Prody