【问题标题】:How to assign different identifiers for different cell styles in one uitableview?如何在一个 uitableview 中为不同的单元格样式分配不同的标识符?
【发布时间】:2012-08-26 06:05:14
【问题描述】:

我是 ios 开发的大一新生,我想在一个 uitableview 的不同部分实现不同的单元格样式。我的表格视图有两个部分,每个部分有三行。

我知道不同的单元格应该有不同的重用标识符来检索重用单元格,但是当我编译我的应用程序时,控制台总是显示以下信息:

2012-08-26 14:04:45.571 Defferent Cell Styles[703:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

任何帮助将不胜感激,我的代码如下:

@end

@implement LGViewController

@synthesize data = _data;
@synthesize list = _list;

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *dataArray = [[NSArray alloc] initWithObjects:@"fwfwf", @"ffgfg", @"sfsfsf", nil];
    NSArray *listArray = [[NSArray alloc] initWithObjects:@"fdff", @"ffdfsw", @"eergerg", nil];
    self.data = dataArray;
    self.list = listArray;
    self.navigationItem.title = @"Data";
}

#pragma mark - DataSource

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return [_data count];
    }
    else if (section == 1){
        return [_list count];
    }
    return section;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *accessoryIdentifier = @"Cells";
    static NSString *switchIdenfier = @"Cells";
    static NSString *sliderIdentifier = @"Cells";

    UITableViewCell *cell;   
    if ((indexPath.section == 0 && indexPath.section == 1) && indexPath.row == 0)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:accessoryIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:accessoryIdentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


    }
    else if ((indexPath.section == 0 && indexPath.section ==1) && indexPath.row == 1)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:switchIdenfier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:switchIdenfier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UISwitch *soundSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
        [soundSwitch addTarget:self action:@selector(showAlert) forControlEvents:UIControlEventValueChanged];
        [soundSwitch setOn:NO animated:NO];
        cell.accessoryView = soundSwitch;


    }
    else if ((indexPath.section ==0 && indexPath.section == 1) && indexPath.row == 2)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:sliderIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sliderIdentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 60, 20)];
        cell.accessoryView = slider;


    }

    return cell;   
}

【问题讨论】:

    标签: ios uitableview reuseidentifier


    【解决方案1】:

    您永远不会返回单元格,因为 indexPath.section 永远不能同时为 0 和 1:

    else if ((indexPath.section ==0 && indexPath.section == 1) && indexPath.row == 2)
    

    应该是

    else if ((indexPath.section ==0 || indexPath.section == 1) && indexPath.row == 2)
    

    在所有情况下。 || 是 OR,&& 是 AND。

    虽然真的,你可以放弃检查部分,因为它总是 0 或 1。

    【讨论】:

      【解决方案2】:

      配置单元的更好方法

       if (indexPath.section == 0) {
          switch (indexPath.row) {
              case 0:
                  //configure you cell here..
                  break;
      
              case 1:
                  //configure you cell here..
                  break;
      
              case 2:
                  //configure you cell here..
                  break;
      
              default:
                  break;
          }
      } else if (indexPath.section == 1) {
          switch (indexPath.row) {
              case 0:
                  //configure you cell here..
                  break;
      
              case 1:
                  //configure you cell here..   
                   break;
      
              case 2:
                  //configure you cell here..                
                  break;
      
              default:
                  break;
          }
      } else if (indexPath.section == 2) {
          switch (indexPath.row) {
              case 0:
              //configure you cell here..                
              break;
      
              case 1:
              //configure you cell here..                
              break;
      
              default:
                  break;
          }
      } 
      }
      return cell;
      

      【讨论】:

      • 非常感谢您的回复!但是如何根据我的 numberOfRowsInSection 方法以您的方式将文本标签设置为两个部分?希望得到您的答复。
      • 你可以在配置单元格部分设置 if (indexPath.section == 0) { switch (indexPath.row) { case 0: cell.textLabel.text = @"PLease add text here"; cell.detailTextLabel.text = @"这里有详细的文字";休息;请参考github.com/gimenete/iOS-boilerplate示例供参考
      • 谢谢!这是一个非常有用的项目。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多