2013-01-16 3 views
-3

나는 contact이라는 사용자 정의 클래스가 있습니다. 이 클래스에는 name, address 등의 속성이 있습니다. contact 유형의 배열을 만들고 UItableview의 사용자 정의 셀에 각 속성 값을 표시하고 싶습니다. 코드 배열에 데이터를 저장하기 :NSString 대신 [myarray objectAtIndex : indexPath.row]에서 객체 가져 오기

[0]->name 
[0]->address 

[1]->name 
[1]->address .... 
:
while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
      // Read the data from the result row 
      NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 
      myContact.nm = aName; 
      NSString *aAddress = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 
      myContact.address = aAddress; 

      NSLog(@"%@",aName);  
      [list.details addObject:myContact]; 
      NSLog(@"%@", list.details); 

} 

여기 자세한 사항

가 .H

내 질문에 @property(nonatomic,retain)NSMutableArray *details;로 선언 된 배열 것은 어떻게 내가 좋아하는 각 인덱스에 값을 검색 할 것입니다 tableview.m에서

내 코드 : 어떻게

cell.name.text = [details objectAtIndex:indexPath.row]; // not working 

내가 해결할 수 있을까?

+2

[details objectAtIndex : indexPath.row] .name을 시도해 보셨습니까? – HKTonyLee

답변

0

[details objectAtIndex:indexPath.row];Contact 개체를 반환합니다. 당신이 필요로하는 것은 nm 코드의 이름을 나타내는 Contact 객체입니다.

이 시도

,

Contact *myContacts = [details objectAtIndex:indexPath.row]; 
cell.name.text = [myContacts nm]; //or [[details objectAtIndex:indexPath.row] nm]; 

마찬가지로 당신은뿐만 아니라 [myContacts address]를 얻을 수 있습니다.

편집 : 코멘트에있는 당신의 질문을 바탕으로 , 대답은 :

while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 

     Contact *myContact = [[Contact alloc] init]; //you should add this here, not outside the loop 
     // Read the data from the result row 
     NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 
     myContact.nm = aName; 
     NSString *aAddress = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 
     myContact.address = aAddress; 

     NSLog(@"%@",aName); 
     [list.details addObject:myContact]; 
     NSLog(@"%@", list.details); 

    } 
+0

나는 그것을 시도했다. 내 실제 databse에는 abc, xyz, ee라는 3 개의 레코드가 있지만 테이블 뷰에는 마지막 레코드 만 3 번 표시됩니다. 예 : ee, ee, ee – z22

+0

지금 내 대답을 확인하십시오. while 루프 안에'Contact * myContact = [[Contact alloc] init ;; ')을 추가해야합니다. – iDev

+0

실제로 while 루프 전에 contact 객체를 초기화했습니다! 고마워. – z22

0

글쎄, 당신은

cell.name.text = [[details objectAtIndex:indexPath.row] name]; 

찾는거야?

+0

나는 이것을 시도했지만 SIGABRT 예외가 발생합니다. 스택 추적 : 'NSInvalidArgumentException', 이유 : '- [연락처 이름] : 알 수없는 선택기가 인스턴스 0x6e00bf0에' – z22

+1

@ z22로 전송되면 캐치되지 않은 예외로 인해 앱이 종료됩니다. 'contact' 클래스에'name '속성이 없습니다. * * 당신이 질문에 명시 했음에도 불구하고. ** 나 자신의 실수를 비난하지 마라. (Objective-C 튜토리얼 **을 읽어 볼 시간. **) –

0

당신이 필요합니다

cell.name.text = [[details objectAtIndex:indexPath.row] nm]; 

이 줄을 사용하는 경우 :

[details objectAtIndex:indexPath.row]; 

을 유형이인 객체를 반환합니다.. 그런 다음 이름을 저장하는 nm 속성을 가져와야합니다. 다음과 같이 사용해야합니다.

[[details objectAtIndex:indexPath.row] nm]; 
1

먼저 클래스의 개체를 만듭니다.

'con'개체에 모든 배열을 저장합니다.

그런 다음,

NSLog("Name :: %@", con.nm); 
NSLog("Addr :: %@", con.address); 

그것은 이름을 특정 연락처 개체의 & 주소를 참조 할 수 있습니다에 의해 사용합니다.

감사합니다.

1
[[details objectAtIndex:indexPath.row] nm]; 

nm에는 개체를 입력하기 만하면됩니다. 이것은 나를 위해 작동합니다.