UITableView在横向中混合内容

时间:2014-03-25 22:36:58

标签: ios objective-c uitableview

我现在整天都在撞墙。我有UITableViewController,它有两个部分:第一个是可选的,取决于是否显示数据;第二个总是有5个单元格。所有单元格都有UILabelUITextField(字段有时候选择器或日期选择器,具体取决于数据)。当我将方向更改为横向时,整个表格变得疯狂,并且在滚动时在行之间以及部分之间混合字段文本。我读过它与重复使用细胞有关,但到目前为止没有任何帮助。即使我保留用于存储字段内容的新数据模型,它仍然会被错放......

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SearchCell *cell = (SearchCell*)[tableView dequeueReusableCellWithIdentifier:@"SearchCell" forIndexPath:indexPath];
cell.valueField.delegate = self;

if (_attributesArray && indexPath.section == 0) {
    cell.titleLabel.text = _attributesArray[indexPath.row][@"name"];
    cell.valueField.text = _attributesArray[indexPath.row][@"value"];

    switch ([_attributesArray[indexPath.row][@"type"] intValue]) {
        case ATTR_INT:{
            cell.valueField.keyboardType = UIKeyboardTypeNumberPad;
        }
            break;
        case ATTR_DEC:{
            cell.valueField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        }
            break;
        case ATTR_DATE:{
            cell.valueField.inputView = self.datePicker;
        }
            break;
        case ATTR_TEXT:{
            cell.valueField.keyboardType = UIKeyboardTypeDefault;
        }
            break;
        case ATTR_BOOL:{
//                cell.valueField.inputView = self.choicePicker;
        }
            break;
    }
}else{
    switch (indexPath.row) {
        case 0:{
            cell.titleLabel.text = @"Name/Link";
            cell.valueField.keyboardType = UIKeyboardTypeDefault;
        }
            break;
        case 1:{
            cell.titleLabel.text = @"Full text";
            cell.valueField.keyboardType = UIKeyboardTypeDefault;
        }
            break;
        case 2:{
            cell.titleLabel.text = @"Uploaded before";
            cell.valueField.inputView = self.datePicker;
        }
            break;
        case 3:{
            cell.titleLabel.text = @"Uploaded after";
            cell.valueField.inputView = self.datePicker;
        }
            break;
        case 4:{
            cell.titleLabel.text = @"Document type";
//                cell.valueField.inputView = self.choicePicker;
        }
            break;
    }
}

return cell;
}

这是一个非常烦人的问题,提前谢谢

更新

我试过这个,但到目前为止没有运气。我将标签和字段值放在两个字典数组中,分为两部分。它现在在纵向和横向方向上混合文本字段值......

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

  if (_attributesArray && indexPath.section == 0) {
      cell = (SearchCell*)[tableView dequeueReusableCellWithIdentifier:@"Attribute" forIndexPath:indexPath];
      if (cell == nil) {
          cell = [[SearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Attribute"];
      }
      cell.titleLabel.text = _attributesArray[indexPath.row][@"name"];
      cell.valueField.text = _attributesArray[indexPath.row][@"value"];
      cell.valueField.delegate = self;

  }else{
      cell = (SearchCell*)[tableView dequeueReusableCellWithIdentifier:@"Property" forIndexPath:indexPath];
      if (cell == nil) {
          cell = [[SearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Property"];
      }
      cell.titleLabel.text = _propertiesArray[indexPath.row][@"name"];
      cell.valueField.text = _propertiesArray[indexPath.row][@"value"];
      cell.valueField.delegate = self;

  }

  return cell;
}

1 个答案:

答案 0 :(得分:0)

也许更好的方法是拥有2个自定义单元格并为其分配2个不同的标识符?

static NSString *oneTypeIdentifier = @"nonFeaturedCells";
static NSString *anotherTypeIdentifier = @"FeaturedCells";

当你创建单元格时......

SomeCell *cell = [tableView dequeueReusableCellWithIdentifier:oneTypeIdentifier];

if(cell==nil)
{
    cell=[[SomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:oneTypeIdentifier];
}