在同一个表视图中使用两种不同类型的自定义单元格?

时间:2013-07-17 09:20:13

标签: iphone ios objective-c uitableview

提前谢谢。 在我的应用程序中,我有一个tableview,其中我必须使用两种不同风格的自定义单元格,我制作两个自定义单元格,并在tableView cellForRowAtIndexPath方法我使用两个标识符为单元格,即使我尝试了两个部分。但它不起作用。它给了我“EXE BAD Excess”或者其他一些错误。下面是我的代码。

错误:thread1_EXE_BAD_Access(代码= 2,地址0 x 0)

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

//CatIdentifier
static NSString *CellIdentiFier = @"CatIdentifier";
static NSString *Cell1IdentiFier = @"CatIdentifier1";

if (indexPath.section == 0)
{
   CommitteCell *cell = ( CommitteCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentiFier];

    if(cell == nil)
    {

       cell = ( CommitteCell *)[[[NSBundle mainBundle] loadNibNamed:@"CommitteeCell" owner:self options:nil] objectAtIndex:0];

    }

    if (indicator == 1)
    {
      cell.lblName.text = str;

    }
    else
    {
      cell.lblName.text = [arrayName objectAtIndex:indexPath.row];
      cell.lblPost.text = [arrayPost objectAtIndex:indexPath.row];
      cell.picimg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[arrayimage objectAtIndex:indexPath.row]]]];

    }

    cell.backgroundView = [[UIImageView alloc]init];

    UIImage *img  = [UIImage imageNamed:@"link-bg 2.png"];

    ((UIImageView *)cell.backgroundView).image = img;


    return cell;
}


else 
{
    Committee2Cell *cell1 = (Committee2Cell  *)[tableView dequeueReusableCellWithIdentifier:Cell1IdentiFier];
    if(cell1 == nil)
    {

        cell1 = (Committee2Cell  *)[[[NSBundle mainBundle] loadNibNamed:@"Committee2Cell" owner:self options:nil] objectAtIndex:0];

    }

    cell1.lblPost1.text = strPost;
    cell1.txtName.text = strName;


    cell1.backgroundView = [[UIImageView alloc]init];

    UIImage *img  = [UIImage imageNamed:@"link-bg 2.png"];

    ((UIImageView *)cell1.backgroundView).image = img;

    return cell1;
 }

}
tableview中的

部分和section方法中的行如下所示。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
 return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 switch (section)
 {
    case 0:
        return [arrayName count]-1;
        break;
    case 1:
        return 1;
        break;
    default:
        break;
  }

  return 0;
 }

如果有人可以告诉我错误在哪里。再次感谢。

数组和标签的数据如下。

- (void)NewsParser:(NSMutableDictionary *)dic    {       NSLog(@“dic =%@”,dic);

  arrayName = [[NSMutableArray alloc]init];
  arrayPost = [[NSMutableArray alloc]init];
  arrayimage= [[NSMutableArray alloc]init];

  strPost = [[NSString alloc]init];
  strName = [[NSString alloc]init];

  strPost = [[dic valueForKey:@"post"]objectAtIndex:8];
  strName = [[dic valueForKey:@"name"]objectAtIndex:8];

  NSLog(@"Name = %@",strName);
  NSLog(@"Post = %@",strPost);


  for(int i=0;i<[dic count]-1;i++)
  {
    [arrayName addObject:[[dic valueForKey:@"name"]objectAtIndex:i]];
    [arrayPost addObject:[[dic valueForKey:@"post"]objectAtIndex:i]];
    [arrayimage addObject:[[dic valueForKey:@"pic"]objectAtIndex:i]];
  }

  NSLog(@"array  = %@",arrayName);
  NSLog(@"array  = %@",arrayPost);
  NSLog(@"array  = %@",arrayimage);

  [table1 reloadData];


}

4 个答案:

答案 0 :(得分:0)

您只能将可执行标识符设为一次。做这样的事情:

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

       static NSString* identifier;
    if(indexPath.section == 0)
        identifier = @"0";
    else
        identifier = @"1";

   self.tableView.dataSource = self;

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];

    if( cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;

if (indexPath.section == 0)
{

    if(cell == nil)
    {

       cell = ( CommitteCell *)[[[NSBundle mainBundle] loadNibNamed:@"CommitteeCell" owner:self options:nil] objectAtIndex:0];

    }

    if (indicator == 1)
    {
      cell.lblName.text = str;

    }
    else
    {
      cell.lblName.text = [arrayName objectAtIndex:indexPath.row];
      cell.lblPost.text = [arrayPost objectAtIndex:indexPath.row];
      cell.picimg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[arrayimage objectAtIndex:indexPath.row]]]];

    }

    cell.backgroundView = [[UIImageView alloc]init];

    UIImage *img  = [UIImage imageNamed:@"link-bg 2.png"];

    ((UIImageView *)cell.backgroundView).image = img;


    return cell;
}

答案 1 :(得分:0)

我认为更简洁的方法是使用具有两种不同类型单元格的容器视图,然后有选择地显示/隐藏与该单​​元格相关的视图。这将更容易编码和维护,但可能会消耗更多的内存。

答案 2 :(得分:0)

  1. 建议使用-objectForKey作为词典:

    [[dic objectForKey:@"post"] objectAtIndex:8];

  2. 确保name / post / pic上有一个NSArray对象,并锁定dic

  3. 并且,在你的for循环中:

    for(int i = 0; i&lt; [dic count] -1; i ++)
    {
            [arrayName addObject:[[dic valueForKey:@“name”] objectAtIndex:i ]];
            [arrayPost addObject:[[dic valueForKey:@“post”] objectAtIndex:i]];
            [arrayimage addObject:[[dic valueForKey:@“pic”] objectAtIndex:i]];
    }

  4. 您确定[dic count] &lt; = [dic objectForKey:@"name"]? 添加nil到数组将崩溃。

    4.您在哪里调用方法-(void)NewsParser:(NSMutableDictionary *)dic;,如果您的数据数组正确,可能[table1 reloadData];崩溃了。

答案 3 :(得分:0)

如果你需要更多帮助,请顺利地按照我的代码顺利完成工作:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(YOUR CONDITION HERE)
       ShareActionViewCell *shareCell;
        NSString *ShareCellId = [NSString stringWithFormat:@"ShareCell%d",indexPath.row];
        shareCell = (ShareActionViewCell *)[tableView dequeueReusableCellWithIdentifier:ShareCellId];
        if(!shareCell) {
            shareCell = [[ShareActionViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ShareCellId];
        }
        shareCell.selectionStyle = UITableViewCellSelectionStyleNone;
        shareCell.ShareTitle.text = [NSString stringWithFormat:@"%@",[tbldata objectAtIndex:indexPath.row]];


    } else {

        CustCell *dataCell;
        NSString *DataCellId = [NSString stringWithFormat:@"DataCell%d",indexPath.row];
        dataCell = (CustCell *)[tableView dequeueReusableCellWithIdentifier:DataCellId];
        if(!dataCell) {
            dataCell = [[CustCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DataCellId];
        }
        shareCell.selectionStyle = UITableViewCellSelectionStyleNone;
        shareCell.ShareTitle.text = [NSString stringWithFormat:@"%@",[tbldata objectAtIndex:indexPath.row]];

}
}