UITableView仅在向上滚动后才会显示

时间:2012-05-29 08:39:16

标签: iphone objective-c xcode uitableview

我有一个带有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

提前致谢, 旅大

2 个答案:

答案 0 :(得分:0)

1st只为您的单元格设计创建一个对象类,然后将其导入.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;

}

答案 1 :(得分:0)

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