簡體   English   中英

UITableView和字母順序

[英]UITableView and alphabetical order

在我的應用程序中,我解析一個XML文件,然后在UITableView中顯示此文件的條目。 我在網絡上發現了如何按字母順序(例如iPhone通訊錄)制作部分,該部分在我的應用中正常工作。 當我點擊indexPath.row的一行時,我想顯示另一個ViewController,在其中我會找到有關我點擊的行的一些信息,但是我遇到了一些問題:當我點擊一行時,變量indexPath.row指向該部分和新視圖控制器中的信息不正確。 我將在此處發布一些屏幕截圖,以顯示您希望我嘗試解釋的內容。

在以下圖片中,您可以查看應用程序的工作方式:

正確

在以下圖片中,您可以看到我的應用程序的錯誤:

錯誤

您可以在圖1中看到名稱是相同的,在圖2中您可以看到名稱是錯誤的。 我猜這取決於變量indexPath.row 我將在此處發布用於創建和填充tableview的代碼:

#import "TableWasteViewController.h"
#import "WasteXmlParser.h"
#import "WasteDetailViewController.h"

@interface TableWasteViewController ()

@property(nonatomic,strong)NSArray *arrayWastes;
@property(nonatomic,strong)NSMutableArray *typeOfWaste;
@property(nonatomic,strong)NSMutableArray *typeOfBin;
@property(nonatomic,strong)NSMutableArray *indexWastes;
@property(nonatomic,strong)NSMutableArray *typeOfWasteBackup;

@end

@implementation TableWasteViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    WasteXmlParser *parser = [[WasteXmlParser alloc]init];
    [parser parseWasteXml];
    self.arrayWastes = [[NSArray alloc]init];
    self.arrayWastes = [parser.arrayWastes mutableCopy];
    self.indexWastes = [[NSMutableArray alloc]init];
    self.typeOfWaste = [[NSMutableArray alloc]init];
    self.typeOfBin = [[NSMutableArray alloc]init];
    for (int i = 0; i < [self.arrayWastes count]; i++) {
        [self.typeOfWaste addObject:[[self.arrayWastes objectAtIndex:i] objectForKey:@"type"]];
        [self.typeOfBin addObject:[[self.arrayWastes objectAtIndex:i]objectForKey:@"place"]];
    }
    for (int i = 0; i < [self.typeOfWaste count]-1; i++) {
        char alphabet = [[self.typeOfWaste objectAtIndex:i] characterAtIndex:0];
        NSString *uniChar = [NSString stringWithFormat:@"%c", alphabet];
        if (![self.indexWastes containsObject:uniChar]) {
            [self.indexWastes addObject:uniChar];
        }
    }
    self.typeOfWasteBackup = [self.typeOfWaste mutableCopy];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.indexWastes count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.indexWastes objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *alphabet = [self.indexWastes objectAtIndex:section];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",alphabet];
    NSArray *wastes = [self.typeOfWaste filteredArrayUsingPredicate:predicate];

    return [wastes count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    UIFont *myFont = [UIFont fontWithName:@"Arial" size:14.0];
    if (cell == nil) {
         cell  = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSString *alphabet = [self.indexWastes objectAtIndex:[indexPath section]];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
    NSArray *wastes = [self.typeOfWaste filteredArrayUsingPredicate:predicate];

    if ([wastes count] > 0) {
        NSString *cellValue = [wastes objectAtIndex:indexPath.row];
        cell.textLabel.font = myFont;
        cell.textLabel.numberOfLines = 2;
        cell.textLabel.text = cellValue;
    }
    return cell;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.indexWastes;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSIndexPath *indexPath = [self.tableWaste indexPathForSelectedRow];
    WasteDetailViewController *vc = segue.destinationViewController;
    vc.typeOfWaste = [self.typeOfWaste objectAtIndex:indexPath.row];
    vc.typeOfBin = [self.typeOfBin objectAtIndex:indexPath.row];
    vc.urlPic = [self.arrayWastes[indexPath.row]objectForKey:@"imgUrl"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)backToHome:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end

希望您能幫助我解決此問題。 謝謝

行按節索引,即每個節中的第一項具有indexPath.row == 0 因此,為了在展平的vc.typeOfWastevc.typeOfBin數組中查找值,您需要按照numberOfRowsInSection方法的方式進行操作,在此方法中,您需要按字母字符過濾展平的數組,然后獲取使用indexPath.row過濾的數組的indexPath.row

總體而言,這種方法似乎很麻煩,必須反復過濾數據。 您的數據結構無法很好地解決要解決的問題。 我建議使用TLIndexPathTools數據模型TLIndexPathDataModel因為它是專為表和集合視圖設計的,可以將數據組織成多個部分,並可以按索引路徑查找項目。 如果願意,將很樂意指導您進行重構。

我幾天前也遇到了這個問題...您需要使一個類集屬性隨心所欲,然后將該對象添加到一個數組中,之后您可以對一個屬性進行數組排序,然后將整個數組排序

#import <Foundation/Foundation.h>

@interface HomeFeed : NSObject

@property (nonatomic, copy) UIImage *ItemImage;
@property (nonatomic, copy) NSString *ItemTitle;
@property (nonatomic, copy) NSString *ItemDate;
@property (nonatomic, copy) NSString *ItemDescription;
@property (nonatomic, copy) NSString *ItemHours;
@property (nonatomic, copy) NSString *ItemID;
@property (nonatomic, copy) NSString *itemDetailUrl;
@property (nonatomic, copy) NSString *itemPerson;
@property (nonatomic, copy) NSString *itemThumbUrl;

@property (nonatomic, assign) int ItemDuration;

@end


#import "HomeFeed.h"

@implementation HomeFeed

@synthesize ItemTitle=_ItemTitle, ItemDate=_ItemDate, ItemImage=_ItemImage,ItemID=_ItemID,ItemDuration=_ItemDuration,ItemDescription,ItemHours=_ItemHours,itemDetailUrl,itemPerson,itemThumbUrl;

@end





 NSArray*arr=[responseString JSONValue];
               NSLog(@"Json Dictionary speakersss : %@",arr);
        NSLog(@"Json arr count speaker : %i",arr.count);

        for (int i=0; i<arr.count; i++) {
            NSDictionary *dict=[[ NSDictionary alloc]init];
            dict=[arr objectAtIndex:i];

            HomeFeed *feed = [[HomeFeed alloc] init];


            feed.ItemTitle = [NSString stringWithFormat:@"%@%@%@",[dict objectForKey:@"firstName"],@" ",[dict objectForKey:@"lastName"]];
            feed.ItemDuration = [[NSString stringWithFormat:@"%@", [dict objectForKey:@"count"]] intValue];

            feed.itemDetailUrl=[dict objectForKey:@"detailsUrl"];
            [self.itemsDataArray addObject:feed];
            [itemTitle addObject:feed.ItemTitle];

        }
        HomeFeed *feed = [[HomeFeed alloc] init];

        NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"ItemTitle" ascending:YES];
        [self.itemsDataArray sortUsingDescriptors:[NSArray arrayWithObject:sorter]];




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SpeakersCustomCell"];
    cell.selectionStyle = NO;
    cell.accessoryType = NO;

    HomeFeed *feed = [self.itemsDataArray objectAtIndex:indexPath.row];

    UIImageView *arrow = (UIImageView *)[cell viewWithTag:3];

    arrow.image = [UIImage imageNamed:@"accessory.png"];


    UILabel *lblLeft = (UILabel *)[cell viewWithTag:1];

    UILabel *lblRight = (UILabel *)[cell viewWithTag:2];

    lblLeft.text=feed.ItemTitle;




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    HomeFeed *feed=(HomeFeed*)[self.itemsDataArray objectAtIndex:indexPath.row];
    self.onlinDetail.strUrl=feed.itemDetailUrl;

    NSString *key = @"OrientationStringValue";
    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:feed.itemDetailUrl forKey:key];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NotifSpeaker1" object:nil userInfo:dictionary];

 }

這是因為用於將數據傳遞到WasteDetailViewController的數組未排序,它們是“ typeOfWaste,typeOfBin和urlPic”。 排序后的數組稱為“浪費”,但僅在numberOfRowsInSection和cellForRowAtIndexPath方法中可用。 您需要向前傳遞廢物數組中的數據,因此與其一直對廢物數組進行排序,不如對它進行排序,只需在加載后對其進行排序。

添加此屬性:

@interface TableWasteViewController ()
@property (strong, nonatomic) NSArray *sortedWastes;
@end

現在在viewDidLoad中

NSString *alphabet = [self.indexWastes objectAtIndex:section];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",alphabet];
self.sortedWastes = [self.typeOfWaste filteredArrayUsingPredicate:predicate];

最后,在prepareForSegue中

vc.typeOfWaste = [self.sortedWastes objectAtIndex:indexPath.row];

您的問題全部來自顯示排序數組的事實,但是用於將數據向前傳遞的數組是未排序數組,因此indexPath完全沒有用。

此外,您的typeOfBin和urlPic也將是錯誤的。 您需要找到一種將所有三個數組鏈接在一起的方法,以便在對一個數組進行排序時對它們全部進行排序。 上面的方法僅使您的typeOfWaste數組保持排序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM