Xcode在详细文本标签上查找表视图部分行

时间:2012-06-28 15:39:01

标签: objective-c uitableview nsmutablearray nsindexpath sections

我有一个使用我的表设置的索引,我可以根据节标题返回textLabel.text的正确数组。我的问题是我有一个单独的数组,我需要在detailTextLabel.text中返回。

它从第一部分的第一个索引开始在每个部分重复。例如:

A

苹果

苹果

鳄鱼

鳄鱼

B

apple

蝙蝠

鳄鱼


我已经生成了索引字符和表头。这就是我必须归还的细胞。

NSString *alphabet = [arrayIndex objectAtIndex:[indexPath section]];
            //---get all of First array's beginning with the letter---
            NSPredicate *predicate =
            [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
            NSArray *firstArray = [First filteredArrayUsingPredicate:predicate]; 
            NSArray *secondArray = [Second filteredArrayUsingPredicate:predicate]; 

    if ([FirstArray count]>0) {
        //---extract the relevant state from the states object---
        NSString *cellValue = [firstArray objectAtIndex:indexPath.row];



      //<--This line throws and error assuming it is trying to find the first letter to return the detail text -->  
    NSString *cellValueA = [secondArray objectAtIndex:indexPath.row];
//Returns value from first section and row in all section//
            //NSString *cellValueA = [Second objectAtIndex:indexPath.row]; 
            cell.textLabel.text = cellValue;
            cell.detailTextLabel.text = cellValueA;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }    

如何找到第一个数组的匹配行以返回第二个数组中的正确索引。任何帮助将非常感激。谢谢: - )


完整代码

-(void)loadSQL {
    // Path to the database
    NSString* dbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"DATABASE_NAME.sqlite"];
    NSLog(@"databasePath: %@",dbPath);
    sqlite3 *database;
    NSString *firstString;
    NSString *secondString;

    // Open the database
    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
        NSString *querySQL = [NSString stringWithFormat: 
                              @"SELECT * FROM songs WHERE items LIKE %@ ", viewItems];

        const char *sql = [querySQL UTF8String];

        sqlite3_stmt *compiledStmt;
        // Fetch all names
        if (sqlite3_prepare_v2(database, sql, -1, &compiledStmt, NULL) == SQLITE_OK) {
            // Append each name
            while (sqlite3_step(compiledStmt) == SQLITE_ROW) {

                const char* cFirst = (char*)sqlite3_column_text(compiledStmt, 2);
                        const char* cSecond = (char*)sqlite3_column_text(compiledStmt, 3);

                if (cFirst == NULL)
                    // There should not be a NULL name
                    NSLog(@"Null name!!");

                else {
                firstString = [NSString stringWithUTF8String:cName];
                            secondString = [NSString stringWithUTF8String:cArtist];

                [First addObject:firstString];
                [Second addObject:secondString];


                    //[First release];
                    //[Second release];
                }
            }
            sqlite3_finalize(compiledStmt); // Cleanup the statement
        }
        else {
            NSLog(@"Error retrieving data from database.");
        }
        sqlite3_close(database);
    }
    else {
        NSLog(@"Error: Can't open database!");
    }

//Creating section with 1st letter of the First field//
    for (int i=0; i<[First count]-1; i++){
        //---get the first char of each state---
        char alphabet = [[First objectAtIndex:i] characterAtIndex:0];
        NSString *uniChar = [NSString stringWithFormat:@"%C", alphabet];
        //---add each letter to the index array---
        if (![arrayIndex containsObject:uniChar])
        {
            [arrayIndex addObject:uniChar];
        }
    }         
}





#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (searching)
        return 1;
    else
        return [arrayIndex count];
}

//---set the title for each section---
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (searching)
        return nil;

    else        
        return [arrayIndex objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (searching)
        return([searchedFirst count]);
    else{
        //return([First count]);
        NSString *alphabet = [songIndex objectAtIndex:section];
        NSPredicate *predicate =[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
        NSArray *firstArray = [First filteredArrayUsingPredicate:predicate];
        return [firstArray count];

    }
}

//---set the index for the table---
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

    if (searching) {
        return nil;
    }

    return arrayIndex;
}


//return the cell info
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    if(searching) {
        cell.textLabel.text =  [searchedFirst objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [searchedSecond objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else{
        //---get the letter in the current section---
        NSString *alphabet = [arrayIndex objectAtIndex:[indexPath section]];
        //---get all states beginning with the letter---
        NSPredicate *predicate =
        [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
        NSArray *firstArray = [First filteredArrayUsingPredicate:predicate]; 
        NSArray *secondArray = [Second filteredArrayUsingPredicate:predicate]; 

        if ([songArray count]>0) {
            NSString *firstValue = [firstArray objectAtIndex:indexPath.row];
            NSString *secondValue = [secondArray objectAtIndex:indexPath.row];

            cell.textLabel.text = firtValue;
            cell.detailTextLabel.text = secondValue;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }

    }
    return cell;


}

2 个答案:

答案 0 :(得分:0)

我不确定我理解你需要什么,但是如果你在给定对象的数组中寻找索引,那么你可以使用:

  

<强> indexOfObject:

     

返回最低索引,其对应的数组值等于给定对象。

   - (NSUInteger)indexOfObject:(id)anObject

ref

答案 1 :(得分:0)

回答有关重组的评论中的问题......

首先,创建一个自定义类:

// CellLabels.h
#import <Foundation/Foundation.h>

@interface CellLabels : NSObject
@property (nonatomic, copy) NSString *firstText;
@property (nonatomic, copy) NSString *secondText;
@end

//CellLabels.m
#import "CellLabels.h"

@implementation CellLabels
@synthesize firstText = _firstText;
@synthesize secondText = _secondText;
@end

创建并初始化一个名为First的{​​{1}},而不是SecondNSMutableArray。替换...

cellData

...与...

[First addObject:firstString];
[Second addObject:secondString];

然后,您的数组过滤通过使用以下方式比较“SELF.firstText”和数组引用的工作原理:

CellLabels *labels = [[CellLabels alloc] init];
labels.first = firstString;
labels.second = secondString;
[cellData addObject:labels];

(好吧,我没有检查以确保所有编译但是它应该给你的想法。)