2009-11-02 59 views
0

我有一個索引表視圖,按照它們的第一個字母組織城市。我工作的是一個NSMutableDictionary是用A,B,C等關鍵字創建的。並將相應的城市添加到它們各自的陣列中。例如:UITableView索引

Y =  (
    Yatesboro, 
    Yeagertown, 
    York, 
    "York Haven", 
    "York New Salem", 
    "York Springs", 
    Youngstown, 
    Youngsville, 
    Youngwood, 
    Yukon 
); 
Z =  (
    Zelienople, 
    Zieglerville, 
    "Zion Grove", 
    Zionhill, 
    Zionsville, 
    Zullinger 
); 

現在,我的表視圖負荷與區段內的正確節數和行和索引控制正常工作與此:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [cities count]; 
} 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    if([searchArray count]==0) 
     return @""; 
    return [searchArray objectAtIndex:section]; 
} 
// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [[cities objectForKey:[searchArray objectAtIndex:section]] count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { 
    NSLog([NSString stringWithFormat:@"clicked index : %i",index]); 
    if (index == 0) { 
     [tableView scrollRectToVisible:[[tableView tableHeaderView] bounds] animated:NO]; 
     return -1; 
    } 
    return index; 
} 

我的問題是現在填充文本表格單元格與每個部分的文本...有關我如何獲取這些信息的任何想法?

回答

1

cellForRowAtIndexPath得到通過同時包含所需的行和段NSIndexPath類型的參數。有關更多詳細信息,請參閱NSIndexPath UIKit Additions

有了這樣說,這可能會在特定情況下工作:

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

    static NSString *cellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:cellIdentifier] autorelease]; 
    } 

    NSString *letter = [searchArray objectAtIndex:indexPath.section]; 
    NSArray *city = [cities objectForKey:letter]; 
    cell.text = [city objectAtIndex:indexPath.row]; 

    return cell; 
} 

不知道如果我這樣做是正確(沒有試過在編譯器),但你可能現在得到的總體思路:)

+0

工作就像一個魅力男人。給我留下了很大的頭痛。謝謝! – rson 2009-11-03 13:56:36