나는 객관적인 C (및 C 일반) 및 iPhone 개발에 익숙하며 자바 섬에서부터 나옵니다. 따라서 배울 수있는 몇 가지 기본 사항이 있습니다. 나를.ocjective-c 공개 메소드에서 반환 값 받기
저는 iOS5에서 바로 뛰어 들고 스토리 보드를 사용하고 싶습니다.
지금은 UITableViewController
에 웹 서비스에서 반환되는 값으로 채워질 목록을 설정하려고합니다. 지금은 일부 mock 객체를 생성하고 목록에 이름을 표시하여 계속 진행할 수 있습니다. 자바에서 오는
, 내 첫 번째 방법은 내 목록에 대한 몇 가지 개체를 생성하는 글로벌 접근 방법을 제공하는 새로운 클래스 생성하는 것입니다 : ...
#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가 내 세 모의 객체를 그 행에 표시 할 것으로 기대합니다. 그러나 빈 상태로 유지되고 NSLog
메서드는 "목록의 0 항목"을 콘솔에 인쇄합니다. 그래서 내가 뭘 잘못하고 있니?
도움을 주시면 감사하겠습니다.
안부 펠릭스
업데이트 1 : 지금 삽입 내 코드에 이미 있었다이 상자 ("셀을 반환" "mockObjects를 반환"과)에 두 개의 리턴 문을 복사 놓쳤다.
또한 세 개의 Project 개체를 초기화해야합니다. – JeremyP
예. 답을 더 명확하게 편집했습니다. 고마워 – EsbenB
아아 알겠습니다. 따라서'alloc'은 시스템에 객체를위한 메모리를 준비하는 힌트 일 뿐이며'init'는 실제로 객체를 채 웁니다. 그렇다면 컴파일러가 초기화되지 않은 객체에 대해 불평하지 않는 이유는 무엇입니까? 그것은 C가 기계 지향적이기 때문입니까? – Felix