編集: @Alladinian は正しい答えを持っています! ビューコントローラーのプロパティを使用している場合、他のメソッドから呼び出す必要がある場合は、必ず割り当ててください。
動的テーブル ビュー セルよりも静的テーブル ビュー セルを使用する有効な理由をまだ見つけていません。iOS プログラミングを始めたとき、テーブル ビューはかなり怖かったです。最初のアプリYIKESで sqlite を使用しました。
そうです、UITableView データ ソースとデリゲートをインポートし、テーブル ビューをパネルに追加してフォローアップする必要があります (それが uiview であり、テーブル ビューをサブビューとして追加できると仮定します)。
とにかくViewController.hにはUITableViewDataSourceとUITableViewDelegateが含まれています。
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
UITableView
次に、 aおよび an のプロパティを追加しますNSMutableArray
。
@property (strong, nonatomic) UITableView* tableView;
@property (strong, nonatomic) NSMutableArray* tableViewContents;
ViewController の .m で:
@synthesize tableView;
@synthesize tableViewContents;
内側ViewDidLoad
:
self.tableViewContents = [NSMutableArray arrayWithObjects:@"Cell 1",@"Cell 2",@"Cell 3",nil];
[self.tableView setDelegate:self]
[self.tableView setDatasource:self]
.m ファイル内:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.tableViewContents.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
index = row;
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
cell.textLabel.text = [tableViewContents objectAtIndex:row];
return cell;
}