我正在创建一个演示应用程序,其中我需要在tableView中使用3个customCell。我可以在所有三行中添加第一个customCell,但是当我添加3个单元格应用程序崩溃时。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Create Rows in a tableView
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"customCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
// First CustomCell
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FirstCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
if (indexPath.row ==1) {
// Second CustomCell
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (indexPath.row == 2) {
// Third CustomCell
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PhoneTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:1];
}
}
return cell;
}
当我运行应用程序时崩溃这是错误消息:Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
答案 0 :(得分:2)
问题出在这里
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PhoneTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:1];
^^^
应该是
cell = [nib objectAtIndex:0];
答案 1 :(得分:0)
问题是 cell = [nib objectAtIndex:1]; 它必须始终是 cell = [nib objectAtIndex:0]; 因为只有一个该名称的笔尖
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"customCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
// First CustomCell
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FirstCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
if (indexPath.row ==1) {
// Second CustomCell
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (indexPath.row == 2) {
// Third CustomCell
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PhoneTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
}
return cell;
}
答案 2 :(得分:0)
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (Your_Condition==1) {
DoctorListCell *cell = (DoctorListCell *)[tableView dequeueReusableCellWithIdentifier:@"DoctorListCell"];
if(cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"iPhone_DoctorListCell"
owner:self
options:nil];
cell = (DoctorListCell *)[nib objectAtIndex:0];
}
return cell;
}
else if(your_Condition==2)
{
ArticleListCell *cell = (ArticleListCell *)[tableView dequeueReusableCellWithIdentifier:@"ArticleListCell"];
if(cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"iPhone_ArticleList"
owner:self
options:nil];
cell = (ArticleListCell *)[nib objectAtIndex:0];
}
return cell;
}
else if(your_condition==3)
{
AnswersCell *cell = (AnswersCell *)[tableView dequeueReusableCellWithIdentifier:@"AnswersCell"];
if(cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"iPhone_AnswersCell" owner:self options:nil];
cell = (AnswersCell *)[nib objectAtIndex:0];
}
return cell;
}
return nil;
}