简体   繁体   English

尝试在iOS中的UITableView中移动UIImageView

[英]Trying to move a UIImageView within a UITableView in iOS

I have developed a simple UITableView that displays a list of items. 我已经开发了一个显示项目列表的简单UITableView。 On top of this UITableView, I have created a selection bar in the form of a UIImageView that moves to whichever row is selected by the user. 在此UITableView之上,我以UIImageView的形式创建了一个选择栏,该选择栏移至用户选择的任何行。 I have created two buttons (one up, one down) which also controls the movement of this selection bar. 我创建了两个按钮(一个向上,一个向下),它们也控制此选择栏的移动。 When the user clicks the up button, the selection bar moves up exactly one row, and when the user clicks the down button, the selection bar moves down exactly one row. 当用户单击向上按钮时,选择栏精确地向上移动一行,而当用户单击向下按钮时,选择栏精确地向下移动一行。 My problem is that when I reach the very top of the table, I want the selection bar to move to the very bottom of the table if the user clicks the up button, and I want the selection bar to go to the very top of the table if the user clicks the down button. 我的问题是,当我到达表格的最上方时,如果用户单击向上按钮,我希望选择栏移动到表格的最底部,并且我希望选择栏位于表格的最上方。用户单击向下按钮时的表格。 Both buttons call the same method (I distinguish between the two buttons based on their tag values). 这两个按钮调用相同的方法(我根据它们的标签值在两个按钮之间进行区分)。 However, when I do this, I am getting the following runtime exception: 但是,执行此操作时,出现以下运行时异常:

2013-06-06 11:34:03.124 SimpleTable[4982:c07] *** Assertion failure in -[UITableViewRowData rectForRow:inSection:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableViewRowData.m:1630
2013-06-06 11:34:03.207 SimpleTable[4982:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath 0x9489850> 2 indexes [0, 10])'

I am not sure why this is happening. 我不确定为什么会这样。 Here is my relevant code which is called by my up and down buttons: 这是我的相关代码,由我的向上和向下按钮调用:

- (IBAction)buttonClicked:(id)sender {

    if([sender tag] == 1){

        if (_index.row == 0) {

            _index = [NSIndexPath indexPathForRow:[_tableData count] inSection:_index.section];

        }

        else
            _index = [NSIndexPath indexPathForRow:_index.row - 1 inSection:_index.section];

         [UIView animateWithDuration:.3 animations:^{
            CGRect rect = [self.view convertRect:[_table rectForRowAtIndexPath:_index] fromView:_table];

            CGFloat floatx = _imageView.frame.origin.x - rect.origin.x;
            _imageView.frame = CGRectMake(rect.origin.x + floatx, rect.origin.y, _imageView.frame.size.width, _imageView.frame.size.height);
        }];

    }

    else if([sender tag] == 2){

         if (_index.row == [_tableData count]) {

            _index = [NSIndexPath indexPathForRow:0 inSection:_index.section];

        }

         else
             _index = [NSIndexPath indexPathForRow:_index.row + 1 inSection:_index.section];

        [UIView animateWithDuration:.3 animations:^{
            CGRect rect = [self.view convertRect:[_table rectForRowAtIndexPath:_index] fromView:_table];

            CGFloat floatx = _imageView.frame.origin.x - rect.origin.x;
            _imageView.frame = CGRectMake(rect.origin.x + floatx, rect.origin.y, _imageView.frame.size.width, _imageView.frame.size.height);
        }];

    }

}

Can anyone see what I'm doing wrong? 谁能看到我在做什么错?

Thanks in advance to all who reply. 在此先感谢所有答复。

if (_index.row == [_tableData count])

Assuming _tableData is the array that feeds your table, the count of the data in there will be one more than the row of your last index, since indexes are zero-based. 假设_tableData是为您的表提供数据的数组,由于索引从零开始,因此其中的数据计数将比上一个索引的行多一。

By this I mean, if there are 10 objects in your array, the last row is row 9. 我的意思是,如果数组中有10个对象,则最后一行是第9行。

So your check needs to be 所以你的支票需要是

if (_index.row + 1 == [_tableData count])

Instead. 代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM