我知道这不是一般做法,但我的应用程序需要此功能。我是iPhone编程的新手。当用户拉/滚动表格屏幕时它会更新我必须停止它。当我拉动它时,数据的顺序也会改变。
- (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 :(得分:-1)
首先,你使用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中并从数组中获取值。这样,您只能在加载视图时点击数据库一次。