尝试在 iOS 中的 UITableView 中移动 UIImageView

问题描述 投票:0回答:1

我开发了一个简单的 UITableView 来显示项目列表。在此 UITableView 之上,我以 UIImageView 的形式创建了一个选择栏,该选择栏移动到用户选择的任何一行。我创建了两个按钮(一个向上,一个向下),它们也控制该选择栏的移动。

当用户单击向上按钮时,选择栏将向上移动一行,当用户单击向下按钮时,选择栏将向下移动一行。我的问题是,当我到达表格的最顶部时,如果用户单击向上按钮,我希望选择栏移动到表格的最底部,并且我希望选择栏移动到表格的最顶部如果用户单击向下按钮,则显示表。两个按钮都调用相同的方法(我根据两个按钮的标签值来区分它们)。但是,当我这样做时,我收到以下运行时异常:

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])'

我不确定为什么会发生这种情况。这是我的相关代码,由我的向上和向下按钮调用:

- (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);
        }];
        
    }
    
}

我做错了什么?

ios uitableview uibutton
1个回答
0
投票

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

假设

_tableData
是为表提供数据的数组,其中的数据计数将比最后一个索引的行多 1,因为索引是从零开始的。

我的意思是,如果数组中有 10 个对象,则最后一行是第 9 行。

所以您的支票需要是

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

相反。

© www.soinside.com 2019 - 2024. All rights reserved.