开发者

Why does my UITableView only show two rows of each section?

开发者 https://www.devze.com 2023-02-02 01:46 出处:网络
I have a UITableView and when I build it only two rows will be displayed. Each section has more than two cells to be displayed, I am confused since they are all done the same?

I have a UITableView and when I build it only two rows will be displayed. Each section has more than two cells to be displayed, I am confused since they are all done the same?

#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 = [[N开发者_JS百科SMutableArray 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


Replace this:

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

with:

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


It looks like you're using only one value for the number of rows per section:

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

Does each section have the same number of rows? If not, you should be using section to grab the right number.

It's what's returned in this method (in your case, [self.listData count]) that determines how many rows to display for that section.

I'd analyze the code some more but it's pretty hard to read... :)

0

精彩评论

暂无评论...
验证码 换一张
取 消