如何使表视图可滚动

时间:2011-10-10 04:52:30

标签: iphone ios cocoa-touch uitableview

如何使UITableView可滚动?
当我向下滚动或向上滚动到tableview区域外时,应用程序相当。我使用tablehandler类作为我的表视图数据源。这是我的tablehandler类的一些代码。

//  TableHandler.h
#import <Foundation/Foundation.h>
@interface TableHandler : NSObject <UITableViewDelegate, UITableViewDataSource>
{
    NSMutableArray * tableDataList;
    //database variables
    NSString *databaseName;
    NSString *databasePath; 
}

@property (nonatomic, retain) NSMutableArray * tableDataList;
- (void) fillList;
- (void) dbPathInit;
- (void) getAllPhysician;
@end

//  TableHandler.m
#import "TableHandler.h"
#import "DoctorItem.h"
#import "DoctorItem.h"
#import <sqlite3.h>
@implementation TableHandler
@synthesize tableDataList;
- (void) fillList { 
    [self dbPathInit];
    [self getAllPhysician]; 
}
-(void)dbPathInit{
    databaseName=@"clinic_directory.db";
    NSArray *documentPaths=
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); 
    NSString *documentsDir=[documentPaths objectAtIndex:0];
    databasePath=[documentsDir stringByAppendingPathComponent:databaseName];
}
-(void)getAllPhysician{
    sqlite3 *database;
        self.tableDataList=[[NSMutableArray alloc]init];
    if(sqlite3_open([databasePath UTF8String],&database)==SQLITE_OK){
        const char *sqlStatement="select d._id, d.title, d.name dname, qualification, c.name cname from doctor d left join category_doctor cd ON cd.doctor_id=d._id LEFT JOIN category c on c._id=cd.category_id order by d.name asc";
        sqlite3_stmt *compiledStatement;
        if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL)==SQLITE_OK) {(sqlite3_step(compiledStatement)==SQLITE_ROW) {
                NSInteger doctorId=(int)sqlite3_column_int(compiledStatement,0);
                NSString *doctorTitle=[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 1)];
                NSString *doctorName=[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 2)];
                NSString *qualification=[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement,3)];
                NSString *category=[NSString stringWithUTF8String:(char*) sqlite3_column_text(compiledStatement,4)];
                DoctorItem *physician=[[DoctorItem alloc]initWithId:doctorId Title:doctorTitle DName:doctorName Qualifications:qualification CName:category];
                [self.tableDataList addObject:physician];
                [physician release];            
            }
        }
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);
}
- (NSInteger) tableView : (UITableView *) tableView numberOfRowsInSection: (NSInteger) section {
    return [self.tableDataList 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];
    }
    DoctorItem *physician=(DoctorItem*)[tableDataList objectAtIndex:indexPath.row];

    UILabel *lbName=[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 290, 25)];
    [lbName setText:physician.DName];
    [cell.contentView addSubview:lbName];
    [lbName release];
    UILabel *lbQualifications=[[UILabel alloc]initWithFrame:CGRectMake(10,40,290,25)];
    [lbQualifications setText:physician.Qualifications];
    lbQualifications.textColor=[UIColor lightGrayColor];
    [cell.contentView addSubview:lbQualifications]; 
    [lbQualifications release];
    UILabel *lbCategory=[[UILabel alloc]initWithFrame:CGRectMake(10,70,290,25)];
    [lbCategory setText:physician.CName];
    lbCategory.textColor=[UIColor lightGrayColor];
    [cell.contentView addSubview:lbCategory];
    [lbCategory release];
    [physician release];
    return cell;
}
-(CGFloat)tableView :(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
    return 100;
}
- (void)dealloc {
    [tableDataList release];
    [super dealloc];
}
@end

3 个答案:

答案 0 :(得分:4)

UITableView是UIScrollView的子类。它会自动滚动。

您的UITableView需要一个数据源对象来提供数据。如果数据源中的数字或行数超过屏幕上可以容纳的数量,则表格将滚动。

如果要以编程方式滚动表,可以使用UIScrollView上的offset属性或UITableView上的某个方法来执行此操作。

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITableView_Class/Reference/Reference.html

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html

答案 1 :(得分:3)

当你增加你的tableview行时,它会自动滚动。无需为tableview包含额外的属性。

例如:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

答案 2 :(得分:1)

您不能通过设置属性左右使其实际可滚动。 查看UITableviewDataSource和UITableviewDelegate协议。 通过实现这些消息,您可以为表视图提供数据,然后根据需要自动滚动。