我正在尝试使用两个单独的部分创建UITableviewController
,其中一个部分包含UIImage
个,另一部分包含UIButton
个部分。这是代码。第一部分工作正常,但是如果我将返回2设置为部分的数量,则应用程序崩溃并出现错误:
'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
这是代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0)
{
return [arrayOfMessages count];
}
else{
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.section==0)
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
balloonView = [[UIImageView alloc] initWithFrame:CGRectZero];
balloonView.tag = 1;
label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.tag = 2;
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
label.font = [UIFont fontWithName:@"Chalkduster" size:14];
UIView *message = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
message.tag = 0;
[message addSubview:balloonView];
[message addSubview:label];
message.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[cell.contentView addSubview:message];
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView = [UIView new];
cell.selectedBackgroundView = [UIView new];
}
else
{
balloonView = (UIImageView *)[[cell.contentView viewWithTag:0] viewWithTag:1];
label = (UILabel *)[[cell.contentView viewWithTag:0] viewWithTag:2];
}
NSString *textAll = [arrayOfMessages objectAtIndex:indexPath.row];
NSString *textMessage = [textAll substringFromIndex:2];
NSString *textType = [textAll substringToIndex:2];
CGSize size = [textMessage sizeWithFont:[UIFont fontWithName:@"Chalkduster" size:14] constrainedToSize:CGSizeMake(240.0f, 480.0f) lineBreakMode:UILineBreakModeWordWrap];
if ([textType isEqualToString:@"ME"]) {
balloonView.frame = CGRectMake(318.0f - (size.width + 28.0f), 2.0f, size.width + 28.0f, size.height + 15.0f);
balloonView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
balloon = [[UIImage imageNamed:@"GreenMessage.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
label.frame = CGRectMake(307.0f - (size.width + 5.0f), 8.0f, size.width + 5.0f, size.height);
label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
label.textColor = [UIColor whiteColor];
} else if ([textType isEqualToString:@"HE"]) {
balloonView.frame = CGRectMake(2.0, 2.0, size.width + 28, size.height + 15);
balloonView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
balloon = [[UIImage imageNamed:@"GrayMessage.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
label.frame = CGRectMake(16, 8, size.width + 5, size.height);
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
label.textColor = [UIColor blackColor];
}
balloonView.layer.cornerRadius = 5;
balloonView.clipsToBounds = YES;
balloonView.image = balloon;
label.text = textMessage;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *body = [arrayOfMessages objectAtIndex:indexPath.row];
CGSize size = [body sizeWithFont:[UIFont fontWithName:@"Chalkduster" size:14] constrainedToSize:CGSizeMake(240.0, 480.0) lineBreakMode:UILineBreakModeWordWrap];
return size.height + 20;
}
我该怎么做?
答案 0 :(得分:1)
错误与各部分无关。你做的是对的。可能导致崩溃的是NSArray arrayOfMessages 。
在第1部分(第二部分)中,根据您的代码,行数为1:
if (section==0)
{
return [arrayOfMessages count];
}
else
{
return 1;
}
但是如果NSArray arrayOfMessages 中没有数据怎么办?
NSString *textAll = [arrayOfMessages objectAtIndex:indexPath.row];
代码将在这里破解。
答案 1 :(得分:1)
问题是当tableview开始创建第2节remove if(indexPath.section == 0)时,你没有创建tableviewcell的实例。除此之外,如果你有基于部分的tableview,你需要存储2维数组。第一个数组用于每个部分的部分和项目。对于insance
self.arrayOfMessages = @[ @[ @"Section_Text1" , @"Section1_Text2" ], @[ @"Section2_Text1" , @"Section2_Text2" , @"Section2_Text3" ] ];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arrayOfMessages[indexPath.section].count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *text = self.arrayOfMessages[indexPath.section][indexPath.row];
///TODO
}
答案 2 :(得分:1)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// Your's code here
}
NSString *textAll = nil;
if (indexPath.section == 0)
{
// fetch array object here
textAll = [arrayOfMessages objectAtIndex:indexPath.row];
}
else if (indexPath.section == 1)
{
// Since your array for section 1 returns only one array's object
// you should fetch it appropriately by specifying the index of the array
textAll = [arrayOfMessages objectAtIndex:"Specify the index of the array you want to fetch here which should be within the the bound of arrayOfMessages"];
}
// configure your cell here
return cell;
}