2012-04-02 88 views
0

我对iPhone开发相对较新,我试图添加uitableview来滚动视图,但它显示Sigabart错误,然后我以编程方式添加并添加了uitableviewdelegate和uitableview数据源给它,添加'noOfRowsInSection'方法的delgate方法不beeing called.c能够帮助我。在滚动视图中添加表视图

@interface Class : UIViewController<UITableViewDelegate,UITableViewDataSource> { 
    UITableView *table; 
    UIScrollView *scrView; 
    NSArray *array; 
} 
@property (nonatomic, retain) IBOutlet UIScrollView *scrView; 
@end 

here is my implementation 

- (void)viewDidload 
{ 
    array = [[NSArray alloc]initWithObjects:@"harsha",@"theja",@"pandu", nil]; 
    [super viewDidUnload]; 
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 260, 360, 220)]; 
    [self.view addSubview:table]; 
    table.delegate = self ; 


} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [array count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    cell.textLabel.text = [array objectAtIndex:indexPath.row]; 
    return cell; 

} 

@end 

我加入虽然Interface Builder中滚动视图,但我做的表格视图编程的,matically的TABL视图显示在屏幕上,但该方法不beeing称为

+1

请向我们展示代码和委托方法:)您是否在interfacebuilder中建立了连接? 控制台用sigbart显示的错误是什么? – 2012-04-02 16:56:50

+0

这有很多可能的原因,如果没有问题代码几乎不可能调试。 – mydogisbox 2012-04-02 17:01:31

+0

在附注上,UITableView是UIScrollView的子类。所以如果你只需要标准的垂直滚动,就使用UITableView。 – cobbal 2012-04-03 10:54:53

回答

3

1:您正在使用的,而不是viewDidLoad中viewDidUnload,

第二:增加tableview.dataSource = self并实现数据源委托方法= )

您正在使用委托而不是数据源=)

+0

谢谢你,我在视图中只做了加载,但我还没有将table.datasource添加到self.Adiing它解决了它。 – SreeHarsha 2012-04-03 11:02:38

+0

有时它很简单=) 很高兴我能帮上忙。 – 2012-04-03 11:30:34

1

你把你的代码中viewDidUnload方法。将这段代码viewDidLoad

- (void)viewDidUnload 
{ 
    array = [[NSArray alloc]initWithObjects:@"harsha",@"theja",@"pandu", nil]; 
    [super viewDidUnload]; 
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 260, 360, 220)]; 
    [self.view addSubview:table]; 
    table.delegate = self ; 
} 

应该是这样:

- (void)viewDidLoad 
{ 
    array = [[NSArray alloc]initWithObjects:@"harsha",@"theja",@"pandu", nil]; 
    [super viewDidUnload]; 
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 260, 360, 220)]; 
    [self.view addSubview:table]; 
    table.delegate = self; 
    table.dataSource = self; 
} 
+0

我已经做到了在视图中只加载,因为你已经问过代码,在我的应用程序中有不同的其他东西向你展示我已经创建了一个类,并粘贴在viewdidunload意外我真的很抱歉。 – SreeHarsha 2012-04-03 10:52:55

+0

阅读我的答案:-)你忘了设置数据源。 – 2012-04-03 10:57:53