在tableview中滚动自定义表格单元格时出错

时间:2014-02-11 10:46:43

标签: ios uitableview

嗨,我是iOS开发的新手我面临着在tableview中滚动自定义单元格的问题。我的表视图有大约7个单元格,但是一旦我在运行后滚动视图,我将只获得如下所示的前5个单元格,但即使其他单元格显示也很难显示,我们无法完全滚动查看这些单元格

这是我的viewController.m代码

#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

请知道我哪里出错了。提前谢谢。

1 个答案:

答案 0 :(得分:1)

您正在使用UIViewController呈现UITable,因为您拥有:@interface ViewController : UIViewController <UItableViewDelegate , UITableViewDataSource>。最好直接使用提供的@interface ViewController : UITableViewController的UITableViewController。如果您有使用UIView的正当理由,那么您需要将委托和dataSource设置为self:

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

您还需要确保UIView上没有任何可滚动的内容,使表格视图像UIScrollView或类似内容一样滚动到页面之外。您可以使用自动布局来帮助您查看内容。

最后一点,您的问题可能是UINavigationController覆盖部分表格的结果。如果您使用的是UIView,则需要向UITable添加内容边距,以确保navigationController不会隐藏行。