Objective c 如何将数组加载到UITableView而不是TableViewController中
我尝试从另一个查看器加载一个数组,并将其放入UITableView,而不是TableViewController。我不知道我该怎么做。现在我有这个代码: CRHCommentSection.mObjective c 如何将数组加载到UITableView而不是TableViewController中,objective-c,ios,xcode,uitableview,Objective C,Ios,Xcode,Uitableview,我尝试从另一个查看器加载一个数组,并将其放入UITableView,而不是TableViewController。我不知道我该怎么做。现在我有这个代码: CRHCommentSection.m #import "CRHCommentSection.h" #import "CRHViewControllerScript.h" @interface CRHCommentSection () @end @implementation CRHCommentSection @synthesize o
#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
#导入
@接口CRHCommentSection:UIViewController
@属性(弱、非原子)IBUITableView*观测表;
@结束
由于您已经多次发布了相同的示例代码,我将为您提供不使用静态的方法。是的,您确实需要在视图控制器而不是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的新视图控制器时-设置“数组”,
在您的案例中,我的案例中的注释搜索结果
_searchResultsViewController = [[SearchResultsViewController alloc] initWithNibName:@"SearchResultsViewController" bundle:nil];
//now set the array on the controller
_searchResultsViewController.searchResults = mySearchResults; //(your array)
这是在实例化视图控制器时传递表视图数组的一种简单方法
此外,命名约定将帮助您理清问题
如果您有用于注释的视图控制器,则应该
评论视频控制器
如果数组包含注释,则应称为注释,以便于阅读
干杯
至于样板文件的其余部分,设置datasource、delegate和cellForRow等,
你在最后一次尝试时得到了这个建议,所以我不再重复了
@比格姆说得很有道理
@interface SearchResultsViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
@界面SearchResultsViewController:UIViewController
必须按照上述协议正确定义视图控制器。您需要设置数据源
[self setDataSource:self];
并将协议添加到接口中
<UITableViewDataSource>
阅读您发布的代码,我不清楚您拥有的和想要的之间的区别。当我运行此操作时,我得到一个空白表。好的。。。我用的是故事板而不是笔尖。但我意识到我的代码中有nib的东西。这是因为我使用了其他代码,但忘记将其取出。我该怎么做故事板。谢谢你的时间。我需要在视图控制器选项卡上连接datasource并委托给我的viewcontroller,还需要删除nib的该部分。很抱歉谢谢你。