iPhone / Objective-C - 在UITableViewCell中重置UIButton的问题

时间:2011-06-28 23:36:23

标签: iphone uitableview uibutton

我在自定义UIButton中有UITableViewCell。我在Interface Builder中添加了这个按钮。

然后我在cellForRowAtIndexPath:

中使用以下代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"GenericCellButton";

    GenericCellButton *cell = (GenericCellButton *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:nil options:nil];

        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (GenericCellButton *)currentObject;
                break;
            }
        }
    }

    [cell.inviteButton addTarget:self action:@selector(inviteButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [cell.inviteButton setTag:indexPath.row];

    // This statement doesn't get called...?
    if ([cell.inviteButton.titleLabel.text isEqualToString:@"Please Wait"]) {
        [cell.inviteButton setEnabled:NO];
        [cell.inviteButton setTitle:@"Please Wait" forState:UIControlStateDisabled];
    }
  ...

这是followTapped:方法:

- (void)followTapped:(id)sender {
    UIButton *button = (UIButton *)sender;
    [button setEnabled:NO];
    [button setTitle:@"Please Wait" forState:UIControlStateDisabled];

    // I then call a service method that then fires off a callback method that refreshes the `tableView`. This changes the button text to "Following".
}

在保存记录方面一切正常。

问题是在触发回调方法之前;如果用户在刷新tableView之前将可见单元格滚出视图,则会将按钮重置为“关注”而不是“请等待”,这意味着如果他们再次点击“关注”按钮会导致问题在我的数据库中。

如果将tableView重新加载,如果滚动到视图之外,如何停止刷新该特定单元格?我尝试在cellForRowAtIndexPath:中添加一些逻辑来检查按钮文本是否为“Please Wait”,但它似乎没有按预期工作。它仍然重置为“关注”。

2 个答案:

答案 0 :(得分:2)

UITableView类通常配置为回收单元格。如果你这样配置,例如通过遵循Apple提供的模板,那么使用单元格来保存任何状态信息都不会起作用。通常的方法是在cellForRowAtIndexPath:方法中恢复所需的状态并将其应用于单元格。因此,请根据保存的状态,使用followTapped:方法保存状态,然后使用cellForRowAtIndexPath:应用[button setEnabled:NO];[buttonSetTitle:@"Please wait"];。希望有所帮助(而且我没有错过问题的重点。)

答案 1 :(得分:1)

- (void)followTapped:(id)sender 
{
UIButton *button = (UIButton *)sender;
[button setEnabled:NO];
[button setTitle:@"Please Wait" forState:UIControlStateDisabled];

selectedIndex = indexPath.row; //create this variable in .h file 

// I then call a service method that then fires off a callback method that refreshes the `tableView`. This changes the button text to "Following".
 }

现在

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"GenericCellButton";

GenericCellButton *cell = (GenericCellButton *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:nil options:nil];

    for (id currentObject in topLevelObjects) {
        if ([currentObject isKindOfClass:[UITableViewCell class]]) {
            cell = (GenericCellButton *)currentObject;
            break;
        }
    }
}

[cell.inviteButton addTarget:self action:@selector(inviteButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell.inviteButton setTag:indexPath.row];

// This statement doesn't get called...?
if (selectedIndex == indexPath.row)
{
    [cell.inviteButton setEnabled:NO];
    [cell.inviteButton setTitle:@"Please Wait" forState:UIControlStateDisabled];
}

...