【问题标题】:I am using 2 tableviews in single screen, one is plane and second one is custom cell. Only one is working我在单个屏幕中使用 2 个表格视图,一个是平面,第二个是自定义单元格。只有一个在工作
【发布时间】:2017-03-02 04:24:14
【问题描述】:
NSString *cellIdentifier = @"cellID";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (tableView==self.monthTableVW)
{
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text=[self.monthArr  objectAtIndex:indexPath.row];
    cell.backgroundColor=[ UIColor colorWithRed:224.0f/255.0f green:188.0f/255.0f blue:113.0f/255.0f alpha:0.5f];
}
if (tableView==self.attendanceTBVW)
{
    customeCell *cell1=[[customeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    cell1=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell==nil)
    {
        NSArray *cellArr=[[NSBundle mainBundle]loadNibNamed:@"customeCell" owner:self options:nil];
        cell=[cellArr objectAtIndex:0];
    }

    cell1.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];

}
return cell;
}

我需要一个表格视图用作下拉菜单,第二个是显示数据,即出席TBVW,即customCell。

【问题讨论】:

    标签: objective-c uitableview custom-cell


    【解决方案1】:

    首先在故事板身份检查器中为不同的表格视图单元格设置不同的单元格标识符。那么

     #import CustomTableViewCell.h
    

    然后拖动这个类中的 dateLbl IBoutlet 属性。然后将自定义单元格转换为这种类型。

    NSString *cellIdentifier = @"cellID1";
    NSString *customCell = @"cellID2";
    
    UITableViewCell *cell;
    
    if (tableView==self.monthTableVW)
    {
    
        if (cell == nil)
        {
            cell = [tableView   dequeueReusableCellWithIdentifier:cellIdentifier];
            //cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        cell.textLabel.text=[self.monthArr  objectAtIndex:indexPath.row];
        cell.backgroundColor=[ UIColor colorWithRed:224.0f/255.0f green:188.0f/255.0f blue:113.0f/255.0f alpha:0.5f];
    }
    if (tableView==self.attendanceTBVW)
    {
    
        //customeCell *cell1=[[customeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        //cell1=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if(cell==nil)
        {
            (CustomTableViewCell *)cell = [tableView   dequeueReusableCellWithIdentifier:customCell];
            NSArray *cellArr=[[NSBundle mainBundle]loadNibNamed:@"customeCell" owner:self options:nil];
            cell=[cellArr objectAtIndex:0];
            cell.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];
    
    
        }
    
        //cell1.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];
    
      }
        return cell;
    }
    

    试试看。希望它会起作用。

    【讨论】:

    • 它显示错误,例如“在 UItableviewCell 类型的对象上找不到属性 dateLbl”
    • 你把 dateLbl 属性放在哪里了?
    【解决方案2】:

    从您的代码看来,您只是为下拉菜单返回单元格。您没有返回用于出席TBVW 的单元格1。请为两个表格视图返回单个单元格。希望对您有所帮助。

    【讨论】:

    • 我们最后只需要返回一个表达式,我尝试了一个通用的表达式,但是它会出现异常错误
    【解决方案3】:

    你能不能让它像为不同的表格视图创建不同的单元格并返回它的单元格个体。

    因为两个表不同,所以你必须为两者返回不同的单元格。

    在您的代码中进行此更改。

    if (tableView==self.monthTableVW) {
        NSString *cellIdentifier = @"cellID";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        cell.textLabel.text=[self.monthArr  objectAtIndex:indexPath.row];
        cell.backgroundColor=[ UIColor colorWithRed:224.0f/255.0f green:188.0f/255.0f blue:113.0f/255.0f alpha:0.5f];
        return cell;
    }
    if (tableView==self.attendanceTBVW) {
        NSString *cellIdentifier = @"customeCell";
        customeCell *cell1=[[customeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        cell1=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        if(cell==nil) {
            NSArray *cellArr=[[NSBundle mainBundle]loadNibNamed:cellIdentifier owner:self options:nil];
            cell=[cellArr objectAtIndex:0];
        }
        cell1.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];
        return cell;
    }
    

    【讨论】:

      【解决方案4】:

      尝试进行此更改检查一下

      if (tableView==self.monthTableVW) {
          NSString *cellIdentifier = @"cellID";
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
      
          if (cell == nil) {
              cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
          }
          cell.textLabel.text=[self.monthArr  objectAtIndex:indexPath.row];
          cell.backgroundColor=[ UIColor colorWithRed:224.0f/255.0f green:188.0f/255.0f blue:113.0f/255.0f alpha:0.5f];
          return cell;
      }
      if (tableView==self.attendanceTBVW) {
          NSString *cellIdentifier = @"customeCell";
          customeCell *cell1=[[customeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
          cell1=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
      
          if(cell==nil) {
              NSArray *cellArr=[[NSBundle mainBundle]loadNibNamed:cellIdentifier owner:self options:nil];
              cell=[cellArr objectAtIndex:0];
          }
          cell1.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];
          return cell;
      }
      

      【讨论】:

      • 做了一些小改动,它对我有用,荣誉..谢谢你!
      【解决方案5】:
       UITableViewCell * raj=[[UITableViewCell alloc]init];
      
      
      
      if (tableView==self.monthTableVW)
      {
          NSString *cellIdentifier = @"cellID";
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
      
          if (cell == nil) {
              cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
          }
          cell.textLabel.text=[self.monthArr  objectAtIndex:indexPath.row];
          cell.backgroundColor=[ UIColor colorWithRed:224.0f/255.0f green:188.0f/255.0f blue:113.0f/255.0f alpha:0.5f];
          raj= cell;
      }
      if (tableView==self.attendanceTBVW)
      {
          NSString *cellIdentifier = @"customeCell";
          customeCell *cell1=[[customeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
          cell1=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
      
          if(cell1==nil) {
              NSArray *cellArr=[[NSBundle mainBundle]loadNibNamed:cellIdentifier owner:self options:nil];
              cell1=[cellArr objectAtIndex:0];
          }
          cell1.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];
        raj= cell1;
      }
      
      return raj;
      

      这个对我有用..非常感谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-02
        • 1970-01-01
        相关资源
        最近更新 更多