开发者

iPhone/Objective-C - Problem with UIButton reset within UITableViewCell

I have a UIButton within a custom UITableViewCell. I added this button in Interface Builder.

I then use the following code within 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];
    }
  ...

This is the followTapped: method:

开发者_C百科- (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".
}

Everything is working just fine in terms of saving the record.

The problem is that before the callback method is fired; if the user scrolls the visible cell out of view before the tableView is refreshed, it's resetting the button back to "Follow" instead of "Please Wait", which means if they tap the "Follow" button again it causes issues in my database.

How can I stop that particular cell from refreshing before the tableView is reloaded if it is scrolled out of view? I tried adding some logic in the cellForRowAtIndexPath: that checks for whether the button text is "Please Wait", but it doesn't seem to be working as expected. It's still resetting back to "Follow".


The UITableView class is usually configured to recycle cells. If you configure it like this, e.g. by following the template provided by Apple, then it's not going to work to use the cell to preserve any state information. The normal approach is to recover whatever state you need in the cellForRowAtIndexPath: method and apply it to the cell. So get your followTapped: method to save the state and then in the cellForRowAtIndexPath: apply [button setEnabled:NO]; and [buttonSetTitle:@"Please wait"]; according to the saved state. Hope that helps (and that I've not missed the point of the question.)


- (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".
 }

Now

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

...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜