2014-05-21 4 views
1

그래서 (.xib 파일을 사용하여) 사용자 정의 셀을 작성하고 사용자 정의 컨트롤러 클래스를 사용하여 링크했으며 셀 식별자를 쓰는 것을 잊지 않았습니다. 나는 또한 내 테이블보기 컨트롤러 프로토 타입 셀에 동일한 셀 식별자를 부여했습니다. 사용자 정의 셀 컨트롤러 클래스에서 나는 단지 .h 파일의 텍스트 레이블에 콘센트가 있습니다. 아래는 내 테이블보기 컨트롤러의 코드입니다. 앱을 실행하면 사용자 정의 셀은 표시되지 않지만 셀을 클릭 할 수 있기 때문에 거기에 셀이 있습니다 (그러나 셀은 흰색 임). 무엇이 잘못 되었나요? 맞춤 셀이 표시되지 않는 이유는 무엇입니까?사용자 정의 셀이 표시되지 않습니다.

사용자 정의가 아닌 기본 셀을 사용하면 모든 것이 정상적으로 작동합니다. 그래서 문제는 제가 어딘가에 내 사용자 정의 셀을 사용하지 않는다는 것입니다.

#import "ListmaniaTableViewController.h" 
#import "ListmaniaTableViewCell.h" 

@interface ListmaniaTableViewController() 

@property (strong, nonatomic) NSMutableArray *tasks; //of task object 

@end 

@implementation ListmaniaTableViewController 

- (void)viewDidLoad{ 
    [super viewDidLoad]; 
    task *task1 = [[task alloc] init]; 
    task1.taskDescription = @"TASK 1"; 
    [self.tasks addObject:task1]; 
    task *task2 = [[task alloc] init]; 
    task2.taskDescription = @"TASK 2"; 
    [self.tasks addObject:task2]; 
    task *task3 = [[task alloc] init]; 
    task3.taskDescription = @"TASK 3"; 
    [self.tasks addObject:task3]; 
} 

- (NSMutableArray *)tasks{ 
    if(!_tasks){ 
     _tasks = [[NSMutableArray alloc] init]; 
    } 
    return _tasks; 
} 

/*- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 

    } 
    return self; 

}*/ 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
    if ([segue.identifier isEqualToString:@"Add New Item"]) { 

    } 
} 

- (void)addNewTask:(task *)newTask{ 
    [self.tasks addObject:newTask]; 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [self.tasks count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    ListmaniaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListmaniaCell" forIndexPath:indexPath]; 
    if(!cell){ 
     [tableView registerNib:[UINib nibWithNibName:@"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:@"ListmaniaCell"]; 
     cell = [tableView dequeueReusableCellWithIdentifier:@"ListmaniaCell" forIndexPath:indexPath]; 
    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(ListmaniaTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    task *task = [self.tasks objectAtIndex:indexPath.row]; 
    NSString *taskLabel = task.taskDescription; 
    cell.taskLabel.text = taskLabel; 
} 

@end 

답변

0

당신이 tableView:willDisplayCell:forRowAtIndexPath: 방법이 코드는 tableView:cellForRowAtIndexPath: 방법에 있어야합니다. 이처럼 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    ListmaniaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListmaniaCell" forIndexPath:indexPath]; 
    if(!cell){ 
     [tableView registerNib:[UINib nibWithNibName:@"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:@"ListmaniaCell"]; 
     cell = [tableView dequeueReusableCellWithIdentifier:@"ListmaniaCell" forIndexPath:indexPath]; 
    } 
    task *task = [self.tasks objectAtIndex:indexPath.row]; 
    NSString *taskLabel = task.taskDescription; 
    cell.taskLabel.text = taskLabel; 

    return cell; 
} 
+0

내가 할 때 아무것도 (난 그냥 다시 시도하고 변화 없음) – crazyshark

1

그러나 viewDidLoad:에서

 [self.tableView registerNib:[UINib nibWithNibName:@"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:@"ListmaniaCell"]; 

.

+0

을 변경하지 않을 것이라고, 시작시 응용 프로그램 충돌 및 콘솔 말한다 " NSException 유형의 알려지지 않은 예외로 종료 " – crazyshark

+0

@crazyshark 나는 나의 대답을 편집했다. – dasdom

+0

충돌했을 때 self.tableView를 사용했습니다. – crazyshark

0

문제점을 파악했습니다. 맞춤형 셀에 이중으로 연결된 콘센트가 있습니다. @dasdom에 의해 제안 나는 또한있는 viewDidLoad에 아래 라인을 이동

[self.tableView registerNib:[UINib nibWithNibName:@"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:@"ListmaniaCell"]; 
관련 문제