3

我有一个带有 UIView 和 UITableView 的故事板。UITableView 具有自定义单元格(带有 xib 的 UITableViewCell)。当我运行应用程序时,只显示表格的第一行,表格的其余部分被隐藏。这些行在屏幕上看不到,但它们按预期响应用户交互。

只有在表格向上滚动后才会显示其余单元格。

可能是什么问题呢?

这是带有表格的 UIView 的代码:

@implementation EldersTableViewController
@synthesize items;
@synthesize tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    EldersDataHendler *edh = [[EldersDataHendler alloc]init];
    NSMutableArray *itemsFromPList = [edh dataForTableFrom:[NSDictionary         dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"eldersData"     ofType:@"plist"]]];
    self.items = itemsFromPList;
    [edh release];
    edh=nil;

}

- (void)viewDidUnload
{
    [super viewDidUnload];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
            interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}


- (IBAction)didPressBackButton:(UIButton *)sender {
    [self.view removeFromSuperview];
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{

    SEObject *cellInfo = [self.items objectAtIndex:indexPath.row];    
    EldersItemCell* cell = (EldersItemCell*)[tableView     dequeueReusableCellWithIdentifier:@"EldersItemCell"];
    if (cell == nil) {

        NSArray* topObjects = [[NSBundle mainBundle] loadNibNamed:@"EldersItemCell" owner:self options:nil];

        for (id obj in topObjects){
            if ([obj isKindOfClass:[EldersItemCell class]]){
                cell = (EldersItemCell*)obj;
                break;
            }
        }


    }
    [cell setObject:cellInfo];
    return cell;
}
#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SEObject *cellInfo = [self.items objectAtIndex:indexPath.row];  
    if (!eldersDetailsViewController){
        eldersDetailsViewController = [[self.storyboard     instantiateViewControllerWithIdentifier:@"elderDetailsVC"] retain];
    }
    eldersDetailsViewController.seObject = cellInfo;
    [self.view addSubview:eldersDetailsViewController.view];
}

- (void)dealloc {
    [eldersDetailsViewController release];
    [tableView release];

    [super dealloc];
}
@end

提前致谢, 卢达

4

2 回答 2

0

问题解决了!非常感谢亲爱的阿鲁阿鲁。带有 xib 的自定义单元格有一些东西。所以我以编程方式创建了一个自定义表格视图单元。这是一个很好的教程

于 2012-05-31T10:16:05.977 回答
0

首先为您的单元设计创建一个对象类,然后将其导入您的 .m 文件中。然后像这样尝试并正确连接表视图委托和数据源

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Displayfriendscell";
Displayfriendscell *cell = (Displayfriendscell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[Displayfriendscell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                  }
// Configure the cell...
[[cell viewWithTag:121] removeFromSuperview];
[[cell viewWithTag:151] removeFromSuperview];
friend *user = [sortedFriendsArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;    
//cell.nameLabel.text = user.contactNAame;
cell.nameLabel.text = user.friendName;
//cell.nameLabel.text =[[sortedFriendsArray objectAtIndex:indexPath.row] objectForKey:@"name"];
//user.friendName=[[sortedFriendsArray objectAtIndex:indexPath.row] objectForKey:@"name"];  
NSLog(@"name is%@",cell.nameLabel.text);

return cell;

}
于 2012-05-29T12:30:49.880 回答