ios - 具有两个部分的 UITableViewController

标签 ios uitableview

我正在尝试制作一个包含两个独立部分的 UITableviewController,一个包含一些 UIImage,另一个包含一个 UIButton。这是代码。第一部分完美运行,但如果我将 return 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;
}

我该怎么做?

最佳答案

- (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;
}

关于ios - 具有两个部分的 UITableViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20927063/

相关文章:

ios - swift 中 double 的错误值

ios - 在其他 View 之上使用 EAGLView 的性能

ios - 在 json 响应 swift 嵌套循环中循环

ios - 如何在设备旋转时重新排列 UItableViewCell subview ?

iphone - 我如何完全自定义 tableview

ios - UISegmentedControl 在值更改时未选择

ios - Xcode 8 中没有这样的模块 'CryptoSwift'

android - Flutter sqflite 应用程序不适用于真正的 ios 设备

ios - 不确定为什么 NSUserdefaults 不保存结果?

arrays - 某些数据更改后如何刷新 TableView?