iphone sdk中的索引tableview

时间:2011-12-27 16:14:37

标签: iphone

我有一个带有8个数组的索引表视图,如

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    [tempArray addObject:@"GEN"];
    [tempArray addObject:@"1SA"];
    [tempArray addObject:@"EST"];
    [tempArray addObject:@"EZE"]; 
    [tempArray addObject:@"NAH"];
    [tempArray addObject:@"JOH"];
    [tempArray addObject:@"COL"];
    [tempArray addObject:@"REV"];    
    return tempArray; 
}

并且我把一切都搞定了,我的问题是当我点击它重定向到另一个页面的单元格时,只有第一个数组值是[tempArray addObject:@"GEN"];内的值,我点击[tempArray addObject:@"1SA"];中的值等等,我得到[tempArray addObject:@"GEN"];的值。我的DidSelectRowAtIndexPath看起来像这样

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ChapterSelectionView *detailViewController = [[ChapterSelectionView alloc] initWithNibName:@"ChapterSelectionView" bundle:nil];

    //detailViewController.firstString = firstString;
    // ...
    // Pass the selected object to the new view controller.
    detailViewController.selectedIndex=indexPath.row;
    detailViewController.selectedCountry = selectedCountry;
    appDelegate.selectedBookIndex=indexPath.row;


    self.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:detailViewController animated:YES];

    [detailViewController release];
}

这是我完整的tableview代码

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {

    return index % 8;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [books count];
}

我的viewdidload看起来像这样

- (void)viewDidLoad {
    [super viewDidLoad];
    l
    appDelegate=(Malayalam_BibleAppDelegate *)[[UIApplication sharedApplication] delegate];
    s";



     books = [[NSMutableArray alloc] init];

    NSArray *biblearray1 = [NSArray arrayWithObjects:@"Genesis",
                                       @"Exodus",
                                       @"Leviticus",
                                       @"Numbers",
                                       @"Deuteronomy",
                                       @"Joshua",
                                       @"Judges",
                                       @"Ruth", nil];
    NSDictionary *bibledic1 = [NSDictionary dictionaryWithObject:biblearray1 forKey:@"Countries"];

    NSArray *biblearray2 = [NSArray arrayWithObjects:@"1Samuel",
                            @"2Samuel",
                            @"1King",
                            @"2King",
                            @"1Chronicles",
                            @"2Chronicles",
                            @"Ezra",
                            @"Nehemiah", nil];
    ......etc etc


    [books addObject:bibledic1];
    [books addObject:bibledic2];
      .....etc etc
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *dictionary = [books objectAtIndex:section];
    NSArray *array = [dictionary objectForKey:@"Countries"];
    return [array count];
}


// Customize the appearance of table view cells.
- (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];

    }
    UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
    myBackView.backgroundColor = [UIColor brownColor];
   // [myBackView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
    cell.selectedBackgroundView = myBackView;
    [myBackView release];
    // Configure the cell.
   // cell.textLabel.tag =row*1+col;

    //First get the dictionary object
    NSDictionary *dictionary = [books objectAtIndex:indexPath.section];
    NSArray *array = [dictionary objectForKey:@"Countries"];
    //NSString *cellValue = [array objectAtIndex:indexPath.row];
    cell.textLabel.text =[array objectAtIndex:indexPath.row];
    cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
    //cell.textLabel.text = [books objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:18.0];
    cell.textLabel.textColor = [UIColor darkGrayColor];
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;;

    return cell;
}

如何从tableview单元格中获取正确的值。 提前致谢。 修改

- (void)viewDidLoad {
    [super viewDidLoad];
  [[self navigationController] setNavigationBarHidden:YES animated:NO];
    scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,49,320,480)];
    appDelegate = (Malayalam_BibleAppDelegate *)[[UIApplication sharedApplication] delegate];
    //self.navigationItem.title=[appDelegate.books objectAtIndex:selectedIndex]; 
    chapterlabel.text = [appDelegate.books objectAtIndex:selectedIndex];
    buttonArray =[[NSMutableArray alloc]initWithCapacity:0];
    //self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg10"]];

        [self.view addSubview:scrollView];


}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [[self navigationController] setNavigationBarHidden:YES animated:NO];

    n=[DbHandler mNumberOfChaptersInBook:[appDelegate.books objectAtIndex:selectedIndex]];
    int scrollViewHieght=n/6;
    scrollView.contentSize = CGSizeMake(320,10+34*scrollViewHieght);

    i=1;

    int rowCount=n/6;
    for(int row=0;row<=rowCount;row++){
        for (int col = 0; col < 6; col++) {
            if(i<=n){
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.titleLabel.textColor=[UIColor blueColor];
                button.titleLabel.font = [UIFont fontWithName:@"Georgia" size:15.0];
                [button setBackgroundImage:[UIImage imageNamed:@"tabs"] forState:UIControlStateNormal];
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                //button.frame=CGRectMake(col*52+5,row*34+50,50,32);
                button.frame=CGRectMake(col*52+5,row*34+0,50,32);
                [button addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
                [button setTitle:[NSString stringWithFormat:@"%d",i++] forState:UIControlStateNormal];
                //button.titleLabel.font=[UIFont boldSystemFontOfSize:15];
                button.titleLabel.textColor=[UIColor blackColor];
                button.tag =row*6+col;
                [buttonArray addObject:button];
                [scrollView addSubview:[buttonArray objectAtIndex:row*6+col]];
                //[self.view addSubview:button];
                [button release];
            }
        }
    }



}

1 个答案:

答案 0 :(得分:0)

您还需要将section section发送到detailViewController。根据您必须选择所需数组的部分。