你可以从UITableView部分索引字符并将其用作别名,以便为Section提供扩展名称吗?

时间:2014-03-18 03:37:37

标签: xcode5 alias uitableview

我有一个UITableView,列出了网站的报告。这些报告来自现有数据库,我无法更改已有的名称。

所有报告名称都以相应的单词开头,以描述报告类型。即POT,CA,年度,审计等。

我已将这些报告名称中的前3个字符编入索引,以将报告划分为相应的部分,但这意味着部分名称为POT,Ann,CA,Aud等。我想要做的是将这些字符编入索引字符并将它们用作别名以将名称扩展为其全名。

这可以轻松完成吗?

编辑显示已经尝试的内容

我尝试为3个字母的索引值设置定义,希望它们会根据该信息进行更改,但这不起作用。

有没有人有任何可以帮助我的想法。如果有另一种方法可以让它发挥作用,我非常乐意彻底改变我的方法。也许有办法从值中删除数字?

UITABLEVIEW的当前代码

#import "reportsTestViewController.h"
#import "ReportsDataObject.h"
#import "Session.h"

@interface reportsTestViewController ()

@end

@implementation reportsTestViewController

@synthesize response;

@synthesize myDataIvar;


#define POT "PATH OF TRAVEL"
#define Ann "ANNUAL PASSIVE"
#define Aud "AUDIT"
#define CA "CONTRACTOR AUDIT"


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

- (void)viewDidLoad
{
    reportsTable.delegate = self;
    reportsTable.dataSource = self;
    self.sections = [[NSMutableDictionary alloc] init];

    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

#pragma mark NSURLConnection Delegate Methods
//    

    //Create your request pointing to the test page
   NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.tesg.com.au/allCustBuild.php"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];


    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    //initialize it when you create your connection
    if (connection){
        self.myDataIvar = [[NSMutableData alloc] init];
    }
}

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
        [self.myDataIvar setLength:0];
    }

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
        [self.myDataIvar appendData:data];

    }

    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
        NSLog(@"Connection Failed: %@", error.userInfo);
    }

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    //this is where you would parse the data received back from the server
    NSString *responseString = [[NSString alloc] initWithData:self.myDataIvar encoding:NSUTF8StringEncoding];
    NSLog(@"Received Data: %@",responseString);
    [self setupReportsFromJSONArray:self.myDataIvar];

}

-(void)connectionWasASuccess:(NSData *)data{
    [self setupReportsFromJSONArray:data];

}



-(void)setupReportsFromJSONArray:(NSData*)dataFromReportsArray{
    BOOL found;
    NSError *error;
   // NSMutableArray *reportsArray = [[NSMutableArray alloc] init];
    NSArray *arrayFromServer = [NSJSONSerialization JSONObjectWithData:dataFromReportsArray options:0 error:&error];

    if(error){
        NSLog(@"error parsing the json data from server with error description - %@", [error localizedDescription]);
    }

    else {
        reportsArray = [[NSMutableArray alloc] init];
        for(NSDictionary *eachReport in arrayFromServer)
        {

            ReportsDataObject *report = [[ReportsDataObject alloc] initWithJSONData:eachReport];

            [reportsArray addObject:report];
            NSString *c = [[eachReport objectForKey:@"title"] substringToIndex:3];
                      found = NO;

            for (NSString *str in [self.sections allKeys])
            {
                if ([str isEqualToString:c])
                {
                    found = YES;
                }
            }

            if (!found)
            {
                [self.sections setValue:[[NSMutableArray alloc] init] forKey:c];
            }
        }

        }
        NSLog(@"Array Populated");
        NSLog(@"%u reports found",reportsArray.count);
        //Now you have your reportsArray filled up with all your data objects



for (NSDictionary *eachReport in arrayFromServer)
{
   [[self.sections objectForKey:[[eachReport objectForKey:@"title"] substringToIndex:3]] addObject:eachReport];
}

// Sort each section array
for (NSString *key in [self.sections allKeys])
{
    [[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]]];


}

[reportsTable reloadData];
}


-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    NSUInteger count = [[self.sections allKeys] count];
    NSLog(@"Number of sections: %d", count);
    return count;
}




- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    {
        NSUInteger count = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
        NSLog(@"Number of rows in section: %d", count);
        return count;
    }
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        return [[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    }





-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{


    static NSString *cellIdentifier = @"Cell";



    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell)
    {


        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

    }

        //The beauty of this is that you have all your data in one object and grab WHATEVER you like
    //This way in the future you can add another field without doing much.
    NSUInteger count = [[self.sections allKeys] count];
    if(count == 0){
        cell.textLabel.text = @"no reports to show";
    }
    else{
        NSDictionary *Reports = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

        cell.textLabel.text = [Reports objectForKey:@"title"];
        cell.detailTextLabel.text = [Reports objectForKey:@"building"];



        // in the future you can grab whatever data you need like this
        //[currentReport buildingName], or [currentReport reportName];
    }


    return(cell);


    }





@end

1 个答案:

答案 0 :(得分:1)

未经测试,但我认为你想要这样的东西:

创建名为NSDictionary的{​​{1}}属性。 首次创建控制器时初始化

titleReference

使用它来填充与您创建的数组匹配的数组。

self.titleReference = [NSDictionary dictionaryWithObjectsAndKeys:
                       @"PATH OF TRAVEL", @"POT",
                       @"ANNUAL PASSIVE", @"Ann",
                       @"AUDIT", @"Aud",
                       @"CONTRACTOR AUDIT", @"CA",
                       nil];