0

我的项目中有一个非常奇怪的行为UITableViewController

通常它可以完美地工作,但在一种特定情况下它不会。

我有一个带有一种自定义类型的单元格的动态表格视图。将所有数据填充到数据源后,表格正确显示所有内容。有一个 Pull-to-Refresh 可以正确更新数据源和表。有一些过滤器按钮可以正确更新带有动画的唯一部分。但是,如果我单击一个详细视图会进入,如果我返回再次单击这些过滤器按钮之一,则所有表格视图单元格都会更新,除了我单击的那些。但是,如果我再次单击此选项,则会出现详细视图,其中包含曾经存在的单元格的数据。所以数据更新得很好,但可见的没有。我将不胜感激任何建议。谢谢

PS:是的,我确实调用deselectRowAtIndexPath:didSelectRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = @"BANF";
BANFCell *cell = (BANFCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier
                                                 forIndexPath:indexPath];
if (cell == nil) {
cell = [[BANFCell alloc] initWithStyle:UITableViewCellStyleDefault
                           reuseIdentifier:CellIdentifier];
}
// collect required data
Requisition *req;
// for right section
if ([self.tableView numberOfSections] == 1) {
    req = [recent objectAtIndex:indexPath.row];
} else {
    if (indexPath.section == 1) {
        req = [recent objectAtIndex:indexPath.row];
    } else {
        req = [notSent objectAtIndex:indexPath.row];
    }
}
NSMutableArray *shortTexts = [[NSMutableArray alloc] init];
// get description text and sort short texts ascending
// also the amount and currency
NSString *reqDescript;
NSString *amount;
NSString *currency;
for (Trait *trait in req.traits) {
    if ([trait.name isEqualToString:@"DESCRIPTION"] && trait.value.length > 0) {
        reqDescript = trait.value;
    }
    if ([trait.name isEqualToString:@"TOTAL_AMOUNT"] && trait.value.length > 0) {
        amount      = trait.value;
    }
    if ([trait.name isEqualToString:@"CURRENCY"] && trait.value.length > 0) {
        currency    = trait.value;
    }
}
NSString *amountAndCurreny;
if (amount) {
    NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
    [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [currencyFormatter setCurrencyCode:currency];
    amountAndCurreny  = [currencyFormatter stringFromNumber:[NSNumber numberWithDouble:amount.doubleValue]];
} else
    amountAndCurreny  = @"";
cell.amountLabel.text = amountAndCurreny;

NSArray *descriptors  = [NSArray arrayWithObjects:[[NSSortDescriptor alloc] initWithKey:@"itm_number"
                                                                              ascending:YES], nil];
NSArray *orderedArray = [req.positions sortedArrayUsingDescriptors:descriptors];
for (Position *position in orderedArray) {
    for (Trait *trait in position.traits) {
        if ([trait.name isEqualToString:@"SHORT_TEXT"] && trait.value.length > 0) {
            [shortTexts addObject:trait.value];
        }
    }
}
UIImage *bgImage = [UIImage imageNamed:@"tableBG"];
cell.backgroundView = [[UIImageView alloc] initWithImage:bgImage];
// filling them in
if (req.iD.integerValue < 0) {
    [cell.histLabel setText:NSLocalizedString(@"New", nil)];
} else {
    [cell.histLabel setText:req.iD.stringValue];
}
[cell.datelabel setText:[labelDateFormatter stringFromDate:req.createDate]];

switch (req.status) {
    case ReqStatusNew:          [cell.imageView setImage:nil];
        break;
    case ReqStatusSaved:        [cell.imageView setImage:[UIImage imageNamed:@"istGespeichertKiste.png"]];
        break;
    case ReqStatusApproved:     [cell.imageView setImage:[UIImage imageNamed:@"genehmigtKiste.png"]];
        break;
    case ReqStatusInWFF:        [cell.imageView setImage:[UIImage imageNamed:@"workflowKiste.png"]];
        break;
    case ReqStatusNotApproved:  [cell.imageView setImage:[UIImage imageNamed:@"abgelehntKiste.png"]];
        break;
    case ReqStatusOrdered:      [cell.imageView setImage:[UIImage imageNamed:@"istBestelltKiste.png"]];
        break;
    case ReqStatusDelivered:    [cell.imageView setImage:[UIImage imageNamed:@"geliefertKiste.png"]];
        break;
}
cell.shortTextLabel.marqueeType = MLContinuous;
cell.shortTextLabel.rate = 50;
cell.shortTextLabel.textAlignment = NSTextAlignmentLeft;
if (reqDescript == nil) {
    cell.shortTextLabel.text = [shortTexts componentsJoinedByString:@", "];
} else if (shortTexts.count > 0) {
    cell.shortTextLabel.text = [NSString stringWithFormat:@"%@: %@", reqDescript, [shortTexts componentsJoinedByString:@", "]];
} else {
    cell.shortTextLabel.text = reqDescript;
}
[cell.shortTextLabel setFrame:CGRectMake(56, 35, 168, 18)];
return cell;
}

viewWillAppear:我只是在导航控制器中设置按钮并调用 [tableview reloadData]viewDidLoad:只是添加刷新控件的委托

刷新控件只是在从 Core Data[tableview reloadData]更新recent和数组之后调用notSent

过滤器按钮只调用:

- (IBAction)filterPressed:(UIButton *)sender {
sender.selected = !sender.selected;
NSArray *filters = [dvFilterList componentsSeparatedByString:@","];
if ([[NSUserDefaults standardUserDefaults] boolForKey:[filters objectAtIndex:sender.tag]]){
    [[NSUserDefaults standardUserDefaults] setBool:NO
                                            forKey:[filters objectAtIndex:sender.tag]];
} else {
    [[NSUserDefaults standardUserDefaults] setBool:YES
                                            forKey:[filters objectAtIndex:sender.tag]];
}
[self updateTableViewData];
// only the section with the recent banfs
NSInteger section = [self numberOfSectionsInTableView:self.tableView] - 1;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section]
              withRowAnimation:UITableViewRowAnimationFade];
}

updateTableViewData是从核心数据更新recentnotSent数组的方法)

4

3 回答 3

0

我终于自己找到了解决方案。在我的didSelectRowAtIndexPath:方法中,我调用performSegueWithIdentifier:并通过将所选行作为发送者变量 Xcode 以某种方式仅保存后台单元格的外观,只能通过从堆栈中删除整个视图控制器来删除。现在我只是self作为发件人提供,因为我不需要变量。所以我编码:

[self performSegueWithIdentifier:@"goToReq" sender:self];

而不是这个:

[self performSegueWithIdentifier:@"goToReq" sender:[self tableView:tableView
                                             cellForRowAtIndexPath:indexPath]];
于 2013-08-16T09:20:25.273 回答
0

你可以试试这个

[tableView reloadData];

你可以把这个写在 cellForRowAtIndexPath

BANFCell *cell = (BANFCell *)[tableView dequeueReusableCellWithIdentifier:nil

                                                         forIndexPath:indexPath];
于 2013-08-15T07:37:15.453 回答
0

我知道这不是原始问题的答案,但可能会帮助其他人看到类似的问题。

我遇到过类似这样的错误代码的行为:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (condition)
    {
        CustomCell1* cell = [tableView dequeueReusableCellWithIdentifier:@"custom1" forIndexPath:indexPath];
        //  configure cell
        //  !! note how "return cell;" is missing !!
    }

    CustomCell2* cell = [tableView dequeueReusableCellWithIdentifier:@"custom2" forIndexPath:indexPath];
    //  configure cell
    return cell; 
}

通过从条件分支实际返回特殊情况单元格来修复。

于 2016-03-23T09:49:06.350 回答