为什么我的UITableView只显示每个部分的两行?

时间:2010-12-31 00:46:43

标签: iphone ios uitableview

我有一个UITableView,当我构建它时,只会显示两行。每个部分都有两个以上的单元格要显示,我很困惑,因为它们都完成了相同的工作?

#import #import "Store.h"
#import "VideoViewController.h"

@implementation Store
@synthesize listData;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [self createTableData];
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    //self.listData = nil;
    //[super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

#pragma mark -
#pragma mark Table View Data Source Methods
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [videoSections count];
}

//Get number of rows
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
    return [self.listData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *StoreTableIdentifier = @"StoreTableIdentifier";

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

    cell.textLabel.text = [[[listData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]
                                   objectForKey:@"name"];

            //Change font and color of tableView
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font=[UIFont fontWithName:@"Georgia" size:16.0];
    cell.textLabel.textColor = [UIColor brownColor];
    return cell;
}

-(NSString *)tableView: (UITableView *)tableView titleForHeaderInSection: (NSInteger) section {
    return [videoSections objectAtIndex:section];
}

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

    videoViewController.detailURL = [[NSURL alloc] initWithString:
                                     [[[listData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]
                                      objectForKey:@"url"]];

    videoViewController.title = [[[listData objectAtIndex:indexPath.section] 
                                  objectAtIndex:indexPath.row] objectForKey:@"name"];

    [self.navigationController pushViewController:videoViewController animated:YES];
    [videoViewController release];
}

#pragma mark Table View Methods

//Data in table cell
-(void) createTableData
{
    NSMutableArray *beginningVideos;
    NSMutableArray *intermediateVideos;

    videoSections = [[NSMutableArray alloc] initWithObjects:
                     @"Beginning Videos", @"Intermediate Videos", nil];

    beginningVideos = [[NSMutableArray alloc] init];
    intermediateVideos = [[NSMutableArray alloc] init];

    [beginningVideos addObject:[[NSMutableDictionary alloc]
                                initWithObjectsAndKeys:@"Shirts", @"name",
                                @"http://www.andalee.com/iPhoneVideos/testMovie.m4v", @"url", nil]];


    [beginningVideos addObject:[[NSMutableDictionary alloc]
                                initWithObjectsAndKeys:@"Posters", @"name",
                                @"http://devimages.apple.com/iphone/samples/bipbopall.html", @"url", nil]];

    [beginningVideos addObject:[[NSMutableDictionary alloc]
                                initWithObjectsAndKeys:@"Stickers",@"name",
                                @"http://www.andalee.com/iPhoneVideos/mov.MOV",@"url",nil]];

    [beginningVideos addObject:[[NSMutableDictionary alloc]
                                initWithObjectsAndKeys:@"Egyptian",@"name",
                                @"http://www.andalee.com/iPhoneVideos/2ndMovie.MOV",@"url",nil]];


    [intermediateVideos addObject:[[NSMutableDictionary alloc]
                                   initWithObjectsAndKeys:@"Drum Solo", @"name", 
                                   @"http://www.andalee.com", @"url", nil]];

    [intermediateVideos addObject:[[NSMutableDictionary alloc]
                                   initWithObjectsAndKeys:@"Veil", @"name", 
                                   @"http://www.andalee.com", @"url", nil]];

    [intermediateVideos addObject:[[NSMutableDictionary alloc]
                                   initWithObjectsAndKeys:@"Three Quarter Shimmy",@"name",
                                   @"http://www.andalee.com",
                                   @"url",nil]];

    listData = [[NSMutableArray alloc] initWithObjects:beginningVideos, intermediateVideos, nil];

    [beginningVideos release];
    [intermediateVideos release];
}

- (void)dealloc {
    [listData release];
    [videoSections release];
    [super dealloc];
    }

@end

2 个答案:

答案 0 :(得分:3)

替换它:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [self.listData count]; 
}

使用:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [[self.listData objectAtIndex:section] count]; 
}

答案 1 :(得分:1)

看起来您每个部分的行数只使用一个值:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [self.listData count]; 
}

每个部分的行数是否相同?如果没有,您应该使用section来获取正确的号码。

这是在此方法中返回的内容(在您的情况下,[self.listData count]),它确定要为该部分显示的行数。

我会更多地分析代码,但很难阅读......:)