私はObjective-C(および一般的にはC)とiPhoneの開発にかなり慣れておらず、Javaアイランドから来ているので、私にとって学ぶのが非常に難しいいくつかの基礎があります。
私はiOS5に飛び込んでいて、ストーリーボードを使いたいと思っています。
今のところUITableViewController
、将来Webサービスから返される値で埋められるリストを設定しようとしています。今のところ、先に進むことができるように、いくつかのモックオブジェクトを生成し、それらの名前をリストに表示したいと思います。
Javaから来て、私の最初のアプローチは、私のリストのいくつかのオブジェクトを生成するためのグローバルにアクセス可能なメソッドを提供する新しいクラスを作成することです。
#import <Foundation/Foundation.h>
@interface MockObjectGenerator : NSObject
+(NSMutableArray *) createAndGetMockProjects;
@end
実装は...
#import "MockObjectGenerator.h"
// Custom object with some fields
#import "Project.h"
@implementation MockObjectGenerator
+ (NSMutableArray *) createAndGetMockObjects {
NSMutableArray *mockProjects = [NSMutableArray alloc];
Project *project1 = [Project alloc];
Project *project2 = [Project alloc];
Project *project3 = [Project alloc];
project1.name = @"Project 1";
project2.name = @"Project 2";
project3.name = @"Project 3";
[mockProjects addObject:project1];
[mockProjects addObject:project2];
[mockProjects addObject:project3];
// missed to copy this line on initial question commit
return mockObjects;
}
そして、これが私のListViewを制御することになっている私のProjectTable.hです
#import <UIKit/UIKit.h>
@interface ProjectsTable : UITableViewController
@property (strong, nonatomic) NSMutableArray *projectsList;
@end
そして最後にProjectTable.m
#import "ProjectsTable.h"
#import "Project.h"
#import "MockObjectGenerator.h"
@interface ProjectsTable {
@synthesize projectsList = _projectsList;
-(id)initWithStyle:(UITableViewStyle:style {
self = [super initWithStyle:style];
if (self) {
_projectsList = [[MockObjectGenerator createAndGetMockObjects] copy];
}
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// only one section for all
return 1;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"%d entries in list", _projectsList.count);
return _projectsList.count;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// the identifier of the lists prototype cell is set to this string value
static NSString *CellIdentifier = @"projectCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Project *project = [_projectsList objectAtIndex:indexPath.row];
cell.textLabel.text = project.name
return cell;
}
したがって、すべてが正しく設定されていると思いますが、tableViewの行に3つのモックオブジェクトが表示されることを期待しています。ただし、空のままで、NSLog
メソッドは「リスト内の0エントリ」をコンソールに出力します。だから私は何が間違っているのですか?
どんな助けでも大歓迎です。
よろしくフェリックス
更新1:すでに私のコードに含まれていた2つのreturnステートメント(「returnmockObjects」と「returncell」)をこのボックスにコピーできず、挿入されました。