Ios UITableView排序

Ios UITableView排序,ios,uitableview,uiimageview,Ios,Uitableview,Uiimageview,我参与了这个项目,以前的开发人员通过使用xib文件,然后注册NIB来定制表格单元格和标题,如下所示: [self.accountTable registerNib:[UINib nibWithNibName:kNonATITableViewCellLandscapeNib bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kNonATITableViewCellLandscapeIdentifier]; [self.accountTabl

我参与了这个项目,以前的开发人员通过使用xib文件,然后注册NIB来定制表格单元格和标题,如下所示:

[self.accountTable registerNib:[UINib nibWithNibName:kNonATITableViewCellLandscapeNib bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kNonATITableViewCellLandscapeIdentifier];

[self.accountTable registerNib:[UINib nibWithNibName:kNonATITableHeaderLandscapeNib bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kNonATITableHeaderLandscapeId];
头文件中有按钮和UIImageView。按钮用于排序,UIImageView用于显示排序方向(asc、desc)的箭头图标。所有按钮和图像视图都是IBOutlet。所有按钮都链接到iAction:

- (IBAction)sortButtonTouched:(id)sender;
该文件还有两个其他属性:

@property (nonatomic, assign) SortType currentSortingOption;
@property (nonatomic, strong) UIButton* btnLastTouched;
这里是SortButtoTouched:

- (IBAction)sortButtonTouched: (UIButton*) buttonTouched {

    if (!self.btnLastTouched) {
        self.btnLastTouched = buttonTouched;
    }

    NSString* strFieldToSort;
    UIImageView* ivSortImage;

    NSArray* arrSortIcons = [[NSArray alloc] initWithObjects:self.ivAccountSort,self.ivNameSort, self.ivAddressSort, self.ivCitySort, self.ivZipSort, self.ivLastCallSort, self.ivMileageSort, nil];

    //get the image for the button selected
    if (buttonTouched.tag == 0) {
        strFieldToSort = @"customerNumber";
        ivSortImage = self.ivAccountSort;
    } else if (buttonTouched.tag == 1) {
        strFieldToSort = @"customerName";
        ivSortImage = self.ivNameSort;
    } else if (buttonTouched.tag == 2) {
        strFieldToSort = @"address";
        ivSortImage = self.ivAddressSort;
    } else if (buttonTouched.tag == 3) {
        strFieldToSort = @"city";
        ivSortImage = self.ivCitySort;
    } else if (buttonTouched.tag == 4) {
        strFieldToSort = @"zip";
        ivSortImage = self.ivZipSort;
    } else if (buttonTouched.tag == 5) {
        strFieldToSort = @"lastCallDate";
        ivSortImage = self.ivLastCallSort;
    } else if (buttonTouched.tag == 6) {
        strFieldToSort = @"mileage";
        ivSortImage = self.ivMileageSort;
    }

    //set the sort option and add icon
    if (!self.currentSortingOption) {

        self.currentSortingOption = SORT_ASC;
        [ivSortImage setImage:[UIImage imageNamed:Ascending_Icon]];

    } else {

        if (![self.btnLastTouched isEqual:buttonTouched]) {
            self.currentSortingOption = SORT_ASC;
            [ivSortImage setImage:[UIImage imageNamed:Ascending_Icon]];

        } else {

            if (self.currentSortingOption == SORT_ASC) {
                self.currentSortingOption = SORT_DESC;
                [ivSortImage setImage:[UIImage imageNamed:Descending_Icon]];
            } else {
                self.currentSortingOption = SORT_ASC;
                [ivSortImage setImage:[UIImage imageNamed:Ascending_Icon]];
            }

        }


    }

    //show and hide
    for(int i=0; i<arrSortIcons.count; i++) {

        UIImageView* ivThisImage = [arrSortIcons objectAtIndex:i];

        if (buttonTouched.tag == i) {
            [UIView animateWithDuration:.25 animations:^(void) {
                ivThisImage.alpha = 1.0;
           }];
        } else {
            [UIView animateWithDuration:.25 animations:^(void) {
                ivThisImage.alpha = 0.0;
            }];
        }

    }



    //call back to routing view controller and sort results based on sort order and field selected
    NSDictionary* dictUserData = [[NSDictionary alloc] initWithObjectsAndKeys:
                                 @"Sort Non-ATI", @"Action",
                                  strFieldToSort, @"Field To Sort",
                                  [NSNumber numberWithLong:self.currentSortingOption], @"Sortng Option",
                                 nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"rvc" object:self userInfo:dictUserData];


    self.btnLastTouched = buttonTouched;


}
现在有两个问题。如果发送通知,图标不会显示。注释掉通知,它们将按预期运行。另一个问题是属性currentSortingOption不保留其值。我认为这两个问题是相关的,但我不是100%确定。重新加载tableview时,是否会再次实例化标头?这对我来说是有意义的,因为uiimageviews将在没有图像的情况下重置,并且属性将丢失其值并重置为0(它是typedef的值)

因此,我是正确的,我如何解决这个问题?如果不是,是什么导致了这些问题


谢谢

好的,很抱歉发帖后马上解决了我的问题,我想有时候你只需要把问题写出来就可以找到解决办法。我所需要做的不是重新加载表,而是重新加载行。以下是更新的方法:

  • (void)SortNonatireResults:(NSDictionary*)dictSortParams{

    如果(self.arrNonATIResults.count>0){

    NSString*sortKey=[dictSortParams objectForKey:@“要排序的字段”];
    //将字段更改为排序以匹配customerInfo对象属性。。。
    NSNumber*numSortType=[dictSortParams objectForKey:@“Sortng选项”];
    BOOL-isAsc=是;
    如果([numSortType intValue]==2){
    isAsc=否;
    }
    NSSortDescriptor*sortDescriptor=[[NSSortDescriptor alloc]initWithKey:sortKey升序:isAsc];
    NSArray*arrsortdescriptor=[[NSArray alloc]initWithObjects:sortddescriptor,nil];
    NSArray*arrSortedNonatireResults=(NSArray*)[self.arrNonatiresResults sortedDarray使用描述符:arrSortDescriptors];
    self.arrNonATIResults=[arrsortednonairesults mutableCopy];
    self.arrDatasource=self.arrnataresults;
    dispatch\u async(dispatch\u get\u main\u queue()^{
    NSMutableArray*indexPathArray=[[NSMutableArray alloc]init];
    对于(NSInteger section=0;section<[self.accountable numberOfSections];++section)
    {
    对于(NSInteger行=0;行<[self.accountable numberofrowsinssection:section];++行)
    {
    [indexPathArray addObject:[NSIndExpathIndExpathForRow:row-inSection:section];
    }
    }
    [self.accountTable重新加载RowsatindExpaths:InExpathArray with RowAnimation:UITableViewRowAnimationNone];
    [self.accountTable ScrollStop];
    });
    
    } }

- (void) sortNonATIResults : (NSDictionary*) dictSortParams {

    if (self.arrNonATIResults.count > 0) {

        NSString* sortKey = [dictSortParams objectForKey:@"Field To Sort"];

        //change the field to sort to match the customerInfo object properties...

        NSNumber* numSortType = [dictSortParams objectForKey:@"Sortng Option"];

        BOOL isAsc = YES;
        if ([numSortType intValue] == 2) {
            isAsc = NO;
        }

        NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:isAsc];

        NSArray* arrSortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

            NSArray* arrSortedNonATIResults = (NSArray*)[self.arrNonATIResults sortedArrayUsingDescriptors:arrSortDescriptors];

            self.arrNonATIResults = [arrSortedNonATIResults mutableCopy];
            self.arrDatasource = self.arrNonATIResults;

            [self.accountTable reloadData];


    }
}
NSString* sortKey = [dictSortParams objectForKey:@"Field To Sort"];

//change the field to sort to match the customerInfo object properties...

NSNumber* numSortType = [dictSortParams objectForKey:@"Sortng Option"];

BOOL isAsc = YES;
if ([numSortType intValue] == 2) {
    isAsc = NO;
}

NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:isAsc];

NSArray* arrSortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    NSArray* arrSortedNonATIResults = (NSArray*)[self.arrNonATIResults sortedArrayUsingDescriptors:arrSortDescriptors];

    self.arrNonATIResults = [arrSortedNonATIResults mutableCopy];
    self.arrDatasource = self.arrNonATIResults;


dispatch_async(dispatch_get_main_queue(), ^{
    NSMutableArray *indexPathArray = [[NSMutableArray alloc] init];
    for (NSInteger section = 0; section < [self.accountTable numberOfSections]; ++section)
    {
        for (NSInteger row = 0; row < [self.accountTable  numberOfRowsInSection:section]; ++row)
        {
            [indexPathArray addObject:[NSIndexPath indexPathForRow:row inSection:section]];
        }
    }

    [self.accountTable reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationNone];

    [self.accountTable scrollsToTop];
});