首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理UITableView的indexPathsForSelectedRows

处理UITableView的indexPathsForSelectedRows
EN

Stack Overflow用户
提问于 2013-06-18 14:29:23
回答 1查看 922关注 0票数 0

我不确定如何才能实现我的模拟UITableView对象正确地响应indexPathsForSelectedRows。在我的应用程序中,用户可以(处于编辑状态)选择表格视图中的单元格,表格视图表示给定目录的文件/文件夹。一旦用户选择了文件夹项,先前选择的文件项就应该被取消选择。我的测试(使用OCHamcrest/OCMockito)如下所示。

代码语言:javascript
复制
- (void)test_tableViewwillSelectRowAtIndexPath_DeselectsPreviouslySelectedCells
{
    // given
    [given(self.mockTableView.editing) willReturnBool:YES];

    // when
    [self.sut tableView:self.mockTableView willSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:SectionIdFile]];
    [self.sut tableView:self.mockTableView willSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:SectionIdFolder]];

    // then
}

问题是,我可以验证文件项是否被选中,但不能向mockTableView询问其选中的行。有人能告诉我怎么处理吗?我是否必须自己记录tableView:selectRowAtIndexPath:animated:scrollPosition:呼叫,并在tableView被要求提供该信息时提供正确答案?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-18 15:58:54

由于mockTableView不能记录(像真正的UITableView一样)所选单元格的索引路径,因此必须确保模拟对象返回该方法的正确答案。因此,在我的例子中,测试现在看起来像这样。

代码语言:javascript
复制
- (void)test_tableViewwillSelectRowAtIndexPath_DeselectsPreviouslySelectedCellsForSectionIdFile
{
    // given
    [given(self.mockTableView.editing) willReturnBool:YES];

    NSArray *selectedRows = @[[NSIndexPath indexPathForRow:0 inSection:SectionIdFile], [NSIndexPath indexPathForRow:1 inSection:SectionIdFile]];
    [given([self.mockTableView indexPathsForSelectedRows]) willReturn:selectedRows];

    // when
    [self.sut tableView:self.sut.myTableView willSelectRowAtIndexPath:selectedRows[0]];
    [self.sut tableView:self.sut.myTableView willSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:SectionIdFolder]];

    // then
    [verify(self.mockTableView) deselectRowAtIndexPath:selectedRows[0] animated:YES];
    [verify(self.mockTableView) deselectRowAtIndexPath:selectedRows[1] animated:YES];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17161942

复制
相关文章

相似问题

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