首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIACollectionView细胞对visibleCells细胞

UIACollectionView细胞对visibleCells细胞
EN

Stack Overflow用户
提问于 2012-12-09 17:39:28
回答 2查看 3.9K关注 0票数 5

我正在使用xcode 4.5中的自动化编写一个测试脚本。

我有一个UICollectionView,我想点击一些当前不可见的单元格。

每份文件,我希望cells返回集合视图中的所有单元格,而visibleCells只返回当前可见的单元格。

相反,我看到的是单元格只返回当前可见的单元格,调用visibleCells将停止'undefined' is not a function (evaluating 'collection.visibleCells()')上的脚本

代码语言:javascript
复制
var target = UIATarget.localTarget();
var collection = target.frontMostApp().mainWindow().collectionViews()[0];

UIALogger.logMessage("Looking in collection: " + collection);
UIALogger.logMessage("Cells: " + collection.cells() + " length " + collection.cells().length);
UIALogger.logMessage("Visible cells: " + collection.visibleCells());

上面的代码返回右边的UICollectionView,第二个日志行打印:

代码语言:javascript
复制
Cells: [object UIAElementArray] length 12

虽然我在集合视图中有100个条目,第三个日志行崩溃脚本。

这是文档/UIACollectionView视图错误吗?

任何想法,我如何告诉自动化滚动,直到它看到一个单元格的名称“我的细胞”?我尝试过使用someCell.scrollToVisible,但是我需要有这个细胞来完成这个任务,但是我没有,因为我无法从细胞中获得它。

编辑:

正如乔纳森建议的那样,我实现了一个直到找到的滚动函数。这有点特定于实现,所以您可能需要调整isCellWithName。我还想添加一个中断,以防我们在while循环中没有找到所需的单元格,如果有人有想法,请随意编辑这个。

代码语言:javascript
复制
function isCellWithName(cell, name) {
    return (cell.staticTexts()[0].name() == name);
}

function getCellWithName(array, name) {
    for (var i = 0; i < array.length; i++) {
        if (isCellWithName(array[i], name)) {
            return array[i];
        }
    }
    return false;
}

function scrollToName(collection, name) {
    var found = getCellWithName(collection.cells(), name);
    while (found === false) {
        collection.dragInsideWithOptions({startOffset:{x:0.2, y:0.99}, endOffset:{x:0.2, y:0},duration:1.0});
        found = getCellWithName(collection.cells(), name);
    }
    return found;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-10 01:47:57

文件显然是不正确的。在visibleCells()上没有UIACollectionView方法。通过循环遍历所有集合视图元素属性并打印出它们的名称,我发现了这一点:

代码语言:javascript
复制
var target = UIATarget.localTarget();
var window = target.frontMostApp().mainWindow();
var collectionView = window.collectionViews()[0];
for (var i in collectionView) {
    UIALogger.logMessage(i);
}

另一方面,表视图元素使用cells()方法列出所有单元格。我想知道他们是否选择不这样做,因为集合视图的性质要复杂得多。实际获取所有集合视图单元格、构建它们的表示形式和框架,并返回元素(如果您有很多元素的话),这可能非常昂贵。这就是UI自动化在询问所有单元格的表视图时所做的工作。它们都必须实例化和计算,才能得到元素表示。

但是,要回答更大的问题,如何滚动到特定的单元格。你能用一个滑动的手势把它一直滚动到视图中吗?这不是最方便的方法,而且我们被滚动到具有表视图的不可见元素的能力“破坏”了。但是从用户行为测试的角度来看,用户无论如何都要滑动一定数量的东西。测试的结构能反映这一点吗?它能满足您的需要吗?

票数 3
EN

Stack Overflow用户

发布于 2013-10-01 21:41:07

我无法让@marmor dragInsideWithOptions()比特以一种通用的方式工作。相反,我正在使用收藏品视图的value()函数来获取当前页面的索引和最后一页的索引,如“11页中的第3页”。然后,我使用收藏品视图的scrollUp()和scrollDown()方法遍历页面,直到找到我们想要的内容为止。我为TuneUpuiautomation-ext.js编写了一个扩展,它似乎起到了作用,还有更多:

代码语言:javascript
复制
function animationDelay() {
    UIATarget.localTarget().delay(.2);
}

extend(UIACollectionView.prototype, {
  /**
   * Apple's bug in UIACollectionView.cells() -- only returns *visible* cells
   */

  pageCount: function() {
    var pageStatus = this.value();
    var words = pageStatus.split(" ");
    var lastPage = words[3];
    return lastPage;
  },

  currentPage: function() {
    var pageStatus = this.value();
    var words = pageStatus.split(" ");
    var currentPage = words[1];
    //var lastPage = words[3];
    return currentPage;
  },

  scrollToTop: function() {
    var current = this.currentPage();
    while (current != 1) {
        this.scrollUp();
        animationDelay();
        current = this.currentPage();
    }
  },

  scrollToBottom: function() {
    var current = this.currentPage();
    var lastPage = this.pageCount();
    while (current != lastPage) {
        this.scrollDown();
        animationDelay();
        current = this.currentPage();
    }
  },

  cellCount: function() {
    this.scrollToTop();
    var current = this.currentPage();
    var lastPage = this.pageCount();
    var cellCount = this.cells().length;
    while (current != lastPage) {
        this.scrollDown();
        animationDelay();
        current = this.currentPage();
        cellCount += this.cells().length;
    }
    return cellCount;
  },

  currentPageCellNamed: function(name) {
    var array = this.cells();
    for (var i = 0; i < array.length; i++) {
        var cell = array[i];
        if (cell.name() == name) {
            return cell;
        }
    }
    return false;
  },

  cellNamed: function(name) {
    // for performance, look on the current page first
    var foundCell = this.currentPageCellNamed(name);
    if (foundCell != false) {
        return foundCell;
    }
    if (this.currentPage() != 1) {
        // scroll up and check out the first page before we iterate
        this.scrollToTop();
        foundCell = this.currentPageCellNamed(name);
        if (foundCell != false) {
            return foundCell;
        }
    }

    var current = this.currentPage();
    var lastPage = this.pageCount();
    while (current != lastPage) {
        this.scrollDown();
        animationDelay();
        current = this.currentPage();

        foundCell = this.currentPageCellNamed(name);
        if (foundCell != false) {
            return foundCell;
        }
    }
    return false;
  },

  /**
   * Asserts that this collection view has a cell with the name (accessibility identifier)
   * matching the given +name+ argument.
   */
  assertCellNamed: function(name) {
    assertNotNull(this.cellNamed(name), "No collection cell found named '" + name + "'");
  }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13789905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档