1

我有一個索引與我的表設置,我能夠根據部分標題返回textLabel.text正確的數組。我的問題是我有一個單獨的數組,我需要在detailTextLabel.text中返回。xocde查找表視圖部分行詳細文本標籤

它只是從第一部分的第一個索引開始的每個部分重複。例如:

蘋果

蘋果

鱷魚

鱷魚

蘋果

蝙蝠

鱷魚


我已經生成索引字符和表頭。這是我必須返回單元格。

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; 


} 
+0

這是一個比回答更元的答案。正因爲這個原因,試圖跟蹤多個數組並將它們通過索引號相關聯通常是一個糟糕的主意。創建一個包含'textLabel'和'detailTextLabel'所需信息的自定義對象以及包含這些對象的單個數組會更好。這樣,一旦你找到了你想要的一個對象,你就可以把所有的信息放在一起。 –

+0

嗨,謝謝。你能舉一個這樣的例子嗎?我是xcode的新手。在這裏我已經從2個字段的sql語句中創建了單獨的數組。我不確定如何添加這兩個。 – Far

+0

如果您可以顯示當前填充「First」和「Second」的代碼,我會嘗試提出改變它的建議。 (基本上,你想創建一個從'NSObject'繼承的新類,並且包含兩個字符串作爲屬性,然後,將這兩個東西放到你的一箇中,而不是向'First'和其他東西添加到'Second'新對象並將其保存到數組中。) –

回答

0

我不知道我知道你需要什麼,但如果你是一個給定對象的數組中尋找索引,那麼你可以使用:

indexOfObject:

返回相應數組值等於給定對象的最小索引。

- (NSUInteger)indexOfObject:(id)anObject 

ref

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 

代替FirstSecond,創建和初始化一個NSMutableArraycellData。替換...

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

...與...

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

然後,你的陣列通過比較 「SELF.firstText」 和你的數組引用作品的過濾工作,通過使用:

CellLabels *labels = [filteredArray objectAtIndex:indexPath.row]; 
cell.textLabel.text = labels.firstText; 
cell.detailTextLabel.text = labels.secondText; 

(好的,我沒有檢查確認所有編譯好的東西,但它應該給你提供這個想法。)

+0

好的,謝謝你。我得到了這個工作,我堅持只是調用labels.firstText數組獲取第一個字母並返回行。我相信我會弄清楚。 :-) – Far

相關問題