使用子类UITableViewCell在UITableView中显示多种类型的视图

时间:2013-10-29 06:09:11

标签: ios objective-c uitableview uiwebview

我有一些静态UIWebView内容我试图加载到UITableView单元格,以及交替单元格中的一些标签。这是我正在尝试(这是行不通的): 在viewDidLoad中:

commentArray = [[NSMutableArray alloc] initWithCapacity:5];

在viewWillAppear中:

 [commentArray removeAllObjects];
 [commentArray addObject:@"sketch here"];
 [commentArray addObject:@"Heading1"];
 [commentsJComments loadHTMLString:htmlString1 baseURL:baseURL];
 [commentArray addObject:commentsJComments];
 [commentArray addObject:@"Heading2"];
 [commentsJComments loadHTMLString:htmlString2 baseURL:baseURL];
 [commentArray addObject:commentsJComments];

在cellForRowAtIndexPath中:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath:

    static NSString *reuseIdentifier = @"MyCell";
    MyTableViewCellSubclass *cell = (MyTableViewCellSubclass *)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell == nil)
    {
        cell = [[MyTableViewCellSubclass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    if (indexPath.row == 0)
     // This image below ends up in every cell of my TableView:
     cell.imageView.image = [UIImage imageNamed:@"Steve_Jobs.jpeg"];
    else {

        if (indexPath.row % 2 == 1)

            cell.descriptionLabel.text = [commentArray objectAtIndex:indexPath.row];
        else {
            NSString *path = [[NSBundle mainBundle] bundlePath];
            NSURL *baseURL = [NSURL fileURLWithPath:path];

            [cell.textWV loadHTMLString:[commentArray objectAtIndex:indexPath.row] baseURL:baseURL];
        }
    }

我的子类TableViewCell:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
   self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
   if (self) {
    // Initialization code
    self.textWV = [[UIWebView alloc] initWithFrame:CGRectMake(9, 0, 300, 400)];
    self.textWV.scrollView.scrollEnabled = NO;
    self.textWV.delegate = self;
    self.descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 10, 300, 30)];
    self.descriptionLabel.textColor = [UIColor blackColor];
    self.descriptionLabel.font = [UIFont fontWithName:@"Arial" size:12.0f];
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 281, 400)];
    self.imageView.image = [UIImage imageNamed:@"Steve_Jobs.jpeg"];
    [self addSubview:self.textWV];
    [self addSubview:self.descriptionLabel];
    [self addSubview:self.imageView];
  }
  return self;
}

2 个答案:

答案 0 :(得分:0)

正确检查您的阵列,然后尝试,我认为您的应用程序崩溃,错误是相关阵列。

答案 1 :(得分:0)

尝试从子类tableview单元格中删除此行self.imageView.image = [UIImage imageNamed:@"Steve_Jobs.jpeg"];,然后重新检查标签和webview的框架。