2012-08-08 5 views
1

다른 뷰어에서로드 한 배열을 가져 와서 TableViewController가 아닌 UITableView에 배치하려고합니다. 어떻게해야할지 모르겠다. 지금은이 코드가 있습니다배열을 TableViewController가 아닌 UITableView로로드하는 방법

CRHCommentSection.m을

#import "CRHCommentSection.h" 
#import "CRHViewControllerScript.h" 

@interface CRHCommentSection() 

@end 

@implementation CRHCommentSection 
@synthesize observationTable; 

NSArray *myArray; 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
} 
return self; 
} 


- (void)viewDidLoad 
    { 


    myArray = [CRHViewControllerScript theArray]; 
    NSLog(@"%@", myArray); 
    //NSArray* paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:1]]; 
    //[observationTable insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop]; 


    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 

    NSLog(@" in method 1"); 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    NSLog(@" in method 2"); 
    // Return the number of rows in the section. 
    return [myArray count]; 


    } 

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

    NSLog(@" in method 3"); 

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    cell.textLabel.text = [myArray objectAtIndex:indexPath.row];  
    return cell; 
} 

CRHCommentSection.h

#import <UIKit/UIKit.h> 


@interface CRHCommentSection : UIViewController 

@property (weak, nonatomic) IBOutlet UITableView *observationTable; 



@end 
+0

게시 한 코드를 읽고, 나는 당신이 갖고있는 것과 당신이 원하는 것의 차이에 대해 명확하지 않습니다. –

+0

이것을 실행하면 빈 테이블이 생깁니다. – Craig

답변

2

당신이 한 번 이상이 같은 예제 코드 자세한 내용을 게시 한 이후, 아픈 당신에게 할 수있는 방법을 제공 이것은 통계를 사용하지 않고. 그리고 네, 당신은 컬렉션 (배열)을 설정해야합니다 UITableView보기 컨트롤러가 아니라 데이터 소스로 컨트롤러를 사용합니다.

은 검색 결과를 표시 jQuery과했다 말할 수 있습니다 ... 그리고 당신은 (당신이 CRHCommentSection입니다)를 정의 당신의 ViewController에서 .. 중 다른보기에서 정도와 같은 응용 프로그램 위임에서

을이보기 컨트롤러 출시 테이블을 채우려는 배열의 속성. 당신의 commentsController에서

또한

@property (nonatomic,retain, setter=setSearchResults:) NSArray *searchResults; //use your array name here 
//BTW please dont call it ARRAY..its confusing. 
(이 당신이 무엇을하는 것이 더 좋은 이름이됩니다) 의 배열
-(void) setSearchResults:(NSArray *)searchResults 
{ 
    //in case we loaded this view with results previously release the previous results 
    if (_searchResults) 
    { 
     [_searchResults release]; 
    } 
    _searchResults = [searchResults retain]; 

    [_tableView reloadData]; 
} 

당신이 그것에 jQuery과 함께 새로운 뷰 컨트롤러의 인스턴스를

을 세터를 추가 - 은 "배열"로 설정, 귀하의 경우 는

_searchResultsViewController = [[SearchResultsViewController alloc] initWithNibName:@"SearchResultsViewController" bundle:nil]; 

//now set the array on the controller 
_searchResultsViewController.searchResults = mySearchResults; //(your array) 

는 배열 F를 전달하는 간단한 방법 그게 전부 내 경우의 SearchResult에 코멘트 또는보기 컨트롤러를 인스턴스화 할 때 테이블보기.

또한 명명 규칙은 덧글에 대한 뷰 컨트롤러가있는 경우 그 의견은 코멘트를 호출 할 필요가 포함되어있는 경우가

CommentsViewController

그리고 배열이

당신이 최대 명확한 일을 도움이 될 것입니다 - 가독성을 위해.

건배.

데이터 소스 및 위임자와 cellForRow 등을 설정하는 나머지 부분에 대해서는 에 대한 조언이 마지막으로 나왔으므로 여기에서 반복하십시오.

@bigkm는

@interface SearchResultsViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> 

귀하의 뷰 컨트롤러는 프로토콜 위와 같이 적절하게 정의 할 수있는 좋은 지적을했다.

+0

좋아요 ... 나는 펜촉이 아닌 스토리 보드를 사용하고 있습니다. 하지만 나는 코드에 펜촉이 필요한 것을 알고 있습니다. 이것은 다른 코드를 사용하고 있기 때문에 잊어 버렸기 때문입니다. 스토리 보드에서 어떻게해야합니까? – Craig

+0

시간 내 주셔서 감사합니다. 데이터 소스와 델리게이트를 View Controller 탭의 뷰 콘트롤러에 연결하고 펜촉의 해당 부분을 삭제해야했습니다. 죄송합니다. 그래도 고마워. – Craig

1

당신은 데이터 소스를

[self setDataSource:self]; 

을 설정하고 또한 인터페이스 프로토콜을 추가해야합니다.

<UITableViewDataSource> 
+0

편집, s/Delegate/DataSource/g, grrrr – bigkm

관련 문제