为什么我的SearchController没有为第一个单元格显示正确的单元格原型?

时间:2015-05-23 18:41:32

标签: ios uitableview swift uisearchcontroller

正如标题所示,我的UISearchController在搜索结果中显示第一个单元格的错误单元格原型时遇到问题。

背景信息:我有两个单元格原型,一个没有图像(标识符:basicCell),另一个带有UIImageView(标识符:imageCell)。不搜索时,细胞工作正常。

问题的详细说明:当我点击搜索栏时,一切都很好,直到我开始搜索某些内容。当我这样做时,第一个单元格总是具有imageCell标识符(灰色空图像视图显示表示缺少图像),无论如何。注意:在搜索任何内容之前,tableview中的第一个单元格都有一个自定义图像...也许这有什么值得注意的? 无论如何,我不知道我做错了什么。有人会介意帮忙吗?

代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if (self.resultSearchController.active) {
            if hasImageAtIndexPath(indexPath) {
                let cell = tableView.dequeueReusableCellWithIdentifier(imageCellIdentifier, forIndexPath: indexPath) as! TimelineTableViewCellImage
                let event = filteredTableData[indexPath.row]

                cell.content.text = profile.content
                cell.name.text = profile.name

                //This is the image
                cell.attachment.image = profile.image
                cell.attachment.layer.cornerRadius = 1
                cell.attachment.clipsToBounds = true

                return cell

            } else {
                let cell = tableView.dequeueReusableCellWithIdentifier(basicCellIdentifier, forIndexPath: indexPath) as! TimelineTableViewCell
                let event = filteredTableData[indexPath.row]

                cell.content.text = profile.content
                cell.name.text = profile.name
                return cell
            }

        } else {
            if hasImageAtIndexPath(indexPath) {
                let cell = tableView.dequeueReusableCellWithIdentifier(imageCellIdentifier, forIndexPath: indexPath) as! TimelineTableViewCellImage
                let event = events[indexPath.row]

                cell.content.text = profile.content
                cell.name.text = profile.name

                cell.attachement.image = profile.image
                cell.attachment.layer.cornerRadius = 1
                cell.attachment.clipsToBounds = true

                return cell

            } else {
                let cell = tableView.dequeueReusableCellWithIdentifier(basicCellIdentifier, forIndexPath: indexPath) as! TimelineTableViewCell
                let event = events[indexPath.row]

                cell.content.text = profile.content
                cell.name.text = profile.name
                return cell

            }

        }

    }

这是我检查图像的代码:

func hasImageAtIndexPath(indexPath:NSIndexPath) -> Bool {
        let event = events[indexPath.row]
        let imageArray = [event.image]
        for eventImage in imageArray {
            if eventImage != nil {
                return true
            }
        }
        return false
    }

1 个答案:

答案 0 :(得分:1)

您需要在hasImageAtIndexPath:函数中使用if-else子句,就像在cellForRowAtIndexPath:中一样。如果表格视图是搜索表格,那么event的定义方式必须与cellForRowAtIndexPath:中的func hasImageAtIndexPath(indexPath:NSIndexPath sender:UITableView) -> Bool if (self.resultSearchController.active){ let event = filteredTableData[indexPath.row] }else{ let event = events[indexPath.row] } let imageArray = [event.image] for eventImage in imageArray { if eventImage != nil { return true } } return false } 相同。

var namedButtonModule = angular.module('namedButtonModule', []);

namedButtonModule.directive('namedButton', function() {
  return {
    restrict: 'AE',
    scope: {
      name: "=?"
    },
    template: '<button class="btn btn-success">I am {{name}} Button</button>',
    controller: function($scope) {
      $scope.name = $scope.name || 'simple';
    }
  };

});

describe('directive: namedButton', function() {

  beforeEach(module('namedButtonModule'));

  beforeEach(inject(function($rootScope, $compile) {
    this.$rootScope = $rootScope;
    this.$compile = $compile;
    this.testContainer = document.getElementById('test-container');
    this.compileDirective = function(template, scope) {
      var element = this.$compile(template)(scope);
      this.testContainer.appendChild(element[0]);
      scope.$digest();
      return element;
    }
  }));

  afterEach(function() {
    this.testContainer.innerHTML = '';
  });

  it('button should show default name', function() {
    var template = '<div named-button></div>';

    var scope = this.$rootScope.$new();

    var element = this.compileDirective(template, scope);
    expect(element.text()).toBe('I am simple Button');
  });

  it('button should show passed name to the scope', function() {
    var template = '<div named-button name="inputName"></div>';

    var scope = this.$rootScope.$new();
    scope.inputName = "Angular Test";

    var element = this.compileDirective(template, scope);
    expect(element.text()).toBe('I am Angular Test Button');
  });
});