当它拖动/滚动时停止重载UITableView中的表数据

问题描述:

我知道这不是一般的做法,但我的应用程序需要此功能。我对iPhone编程非常陌生。当用户拖动/滚动表格屏幕时,它会被更新,我必须停止它。当我拉它时,数据的顺序也会改变。当它拖动/滚动时停止重载UITableView中的表数据

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{

static NSString *MyIdentifier = @"MyIdentifier"; 
cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 300, 50)]; 
    [button addTarget:self action:@selector(optionButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [button setTitle:optionValue forState:UIControlStateNormal]; 
    [button setTag:indexPath.row]; 
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    [cell.contentView addSubview:button]; 
    [button release]; 
} 
return cell; 

}

+0

请向我们展示您的cellForRowAtIndexPath方法。另外,告诉我们为什么你不想更新表格行。如果你做正确的事情不应该导致任何问题。 – sosborn

+0

我已经更新了cellForRowAtIndexPath方法的代码。所有来自这些单元格的数据都来自数据库。而且我不想在用户滚动时重新加载数据。当数据刷新数据的顺序获取更改我不希望它。 –

首先,您使用的cellForRowAtIndexPath的方式是错误的。

static NSString *MyIdentifier = @"MyIdentifier"; 
cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 300, 50)]; 
    [button addTarget:self action:@selector(optionButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    [cell.contentView addSubview:button]; 
    [button release]; 
} 
//These should be outside of the cell == nil loop so that they get refreshed correctly 
[button setTitle:optionValue forState:UIControlStateNormal]; 
[button setTag:indexPath.row]; 

return cell; 

其次,我没有看到你设置optionValue,但如果你不希望每次都击中了数据库,然后将其存储在一个NSArray的值,并从阵列获取值。这种方式只能在加载视图时触击数据库一次。

+0

我已经从上面的方法中删除了设置optionValue的代码。现在一切工作正常,当我删除行if(cell == nil)。我使用NSMutable数组,每次感谢我都没有击中数据库。 –

+0

我已经尝试过但它不起作用,即不正确刷新单元格。谢谢。 –

+0

告诉我更多信息,我可能会提供帮助。它如何失败? – sosborn