i70ro
i70ro

Reputation: 111

Error with scrolling custom table cells in a tableview

Hi I'm a newbie to iOS development I'm facing a problem regarding scrolling of custom cells in a tableview. My table view has around 7 cells but once I scroll the view after running I will be getting only first 5 cells like the below but even tough the rest of the cells are being displayed we can't completely scroll to have a look at those cell.

Here's my viewController.m code

#import "ViewController.h"
#import "MobileTableCell.h"

@interface ViewController ()
{
    NSArray *tableData;
    NSArray *thumbnails;

}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    tableData = [NSArray arrayWithObjects:@"Iphone 5s",@"Google Nexus 5", @"Samsung Galaxy S4",@"HTC one", @"LG G2", @"Moto X", @"Micromax Turbo", nil];

    thumbnails = [NSArray arrayWithObjects:@"iphone5.jpg",@"nexus5.png",@"galaxys4.jpg",@"htcone.jpg",@"lgg2.jpg",@"motox.png",@"micromaxcanvasturbo2.jpg", nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableData count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

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

    MobileTableCell *cell = (MobileTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    //NSLog(@"%@",cell);
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MobileTableCell" owner:self options:Nil];
        cell = [nib objectAtIndex:0];
    }
//    cell.layer.shouldRasterize = YES;
    cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 78;
}
@end

Please lemme know where I have gone wrong. Thanks in advance.

Upvotes: 2

Views: 724

Answers (1)

Mika
Mika

Reputation: 5845

You are using a UIViewController to render the UITable because you have: @interface ViewController : UIViewController <UItableViewDelegate , UITableViewDataSource>. Better to directly use the UITableViewController provided @interface ViewController : UITableViewController. If you have a valid reason for using a UIView then you need to set the delegate and dataSource to self:

tableView.delegate = self;
tableView.dataSource = self;

You also need to make sure there isn't anything else that is scrollable on the UIView that makes the table view scroll out of the page like a UIScrollView or something like that. You can use auto layout to help keep your stuff in view.

Last point, your issue might be the result of a UINavigationController covering parts of the table. If you are using a UIView you need to add content margin to the UITable to make sure the navigationController does not hide rows.

Upvotes: 1

Related Questions