2012-08-08 43 views
1

我想从另一个查看器中加载数组,并将其放入UITableView中,而不是TableViewController。我不知道我会怎么做。现在,我有这样的代码:如何将数组加载到UITableView中,而不是TableViewController

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

阅读你发布的代码,我不清楚你有什么和你想要的之间的区别。 – 2012-08-08 21:14:59

+0

当我运行这个时,我得到一张空白表。 – Craig 2012-08-08 21:19:53

回答

2

既然你已经张贴此相同的示例代码不止一次,我给你做的方式这不使用静态。是的,你需要在视图控制器上设置集合(数组)而不是UITableView,因为tableView使用控制器作为数据源。

比方说,你有这样的显示的搜索结果的UITableView ...你无论从一些其他的角度还是从应用程序委托像这样推出这个视图控制器..

在你的viewController(你是CRHCommentSection)定义你将要用来填充表的数组的属性。

@property (nonatomic,retain, setter=setSearchResults:) NSArray *searchResults; //use your array name here 
//BTW please dont call it ARRAY..its confusing. 

而且在commentsController(这将是一个更好的名字,你有什么)添加setter方法 阵列

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

当你实例与UITableView的新视图控制器它 - 设置“阵列”, 你的情况评论在我的情况SearchResult所

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

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

那是一个简单的方法来沟通数组f的条件或实例化视图控制器时的表视图。

另外的命名约定将帮助您清楚的事情了

如果您有意见视图控制器应该

CommentsViewController

和数组,如果它包含注释应该叫做评论 - 为了可读性。

干杯。

至于其余的样板设置数据源和委托和cellForRow等, 你得到了最后四处的建议,所以我不会在这里重复。

@bigkm具有良好的点

@interface SearchResultsViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> 

你的视图控制器必须被如以上所述的协议正确定义。

+0

好的...我使用storyboard不是笔尖。但我意识到我的代码中有笔尖。这是因为我正在使用其他代码,并忘记了这一点。我怎么做这个故事板。 – Craig 2012-08-08 21:43:54

+0

谢谢你的时间。我需要将数据源和委托连接到视图控制器选项卡上的viewcontroller,并删除该部分的nib。抱歉。不过谢谢。 – Craig 2012-08-08 21:52:50

1

您需要设置数据源

[self setDataSource:self]; 

同时,也是协议添加到您的接口。

<UITableViewDataSource> 
+0

edit,s/Delegate/DataSource/g,grrrr – bigkm 2012-08-08 21:35:45

相关问题