質問

So I have a TableView that loads customTableCells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier1 = @"ReviewTableCell";
    reviewCell = [self.reviewTableView dequeueReusableCellWithIdentifier:CellIdentifier1];

    if (reviewCell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ReviewTableCell" owner:self options:nil];
        reviewCell = [nib objectAtIndex:0];
        [reviewCell.replyButton addTarget:self action:@selector(reply:) forControlEvents:UIControlEventTouchUpInside]; <----- note! reply button function
    }

    reviewCell.replyButton.tag = indexPath.row;

    if ([[_isExpandList objectAtIndex:indexPath.row]isEqual:@1])
    {
        [reviewCell.postView setHidden:NO];
    }
    else if ([[_isExpandList objectAtIndex:indexPath.row]isEqual:@0])
    {
        [reviewCell.postView setHidden:YES];
    }
    return reviewCell;
}

As you can see on the customTableCell, there is a button that has the following function:

- (void)reply:(UIButton *)sender {

    BOOL isExpand = [_isExpandList[sender.tag] boolValue];
    [_isExpandList replaceObjectAtIndex:sender.tag withObject:@(!isExpand)];

    if ([[_isExpandList objectAtIndex:sender.tag]isEqual:@1])
    {
        [self.reviewTableView beginUpdates];
        [reviewCell.postView setHidden:NO];
        [self.reviewTableView endUpdates];
    }
    else
    {
        [self.reviewTableView beginUpdates];
        [reviewCell.postView setHidden:YES];
        [self.reviewTableView endUpdates];
    }
}

When the user presses the button, the sender cell will "expand" (height will extend) and review.postView should appear. However, the cell only expands but the postView does not appear until I scroll down and scroll back up. This is probably because the cell is then reloaded. How can I ensure that the postView appears as soon as I press the button.

_isExpandList is just an array that keeps track of which cell is expanded and which is not. I've initiated it with the following code in ViewDidLoad:

_isExpandList = [[NSMutableArray alloc] init];
for (int i = 0; i < 5; i++) {
    [_isExpandList addObject:@NO];
}

I will include the cellHeight function just in case but I don't think it is relevant.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[_isExpandList objectAtIndex:indexPath.row]isEqual:@0])
    {
        return 100;
    }
    else{
        return  300;
    }
}
役に立ちましたか?

解決

You need to explicitly reload the tableview cell using:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

or

- (void)reloadData

if you don't mind the performance hit of reloading all of the cells.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top