简体   繁体   English

如何使表格视图可滚动

[英]How to make Table view scrollable

How do I make UITableView scrollable? 如何使UITableView可滚动?
When I scroll down or scroll up outside of the tableview area,application quite. 当我向下滚动或向上滚动到tableview区域之外时,应用程序相当。 I use tablehandler class for my table view data source.Here is some of code from my tablehandler class. 我将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

UITableView is a subclass of UIScrollView. UITableView是UIScrollView的子类。 It will scroll for you automagically. 它将自动为您滚动。

Your UITableView will need a data source object to feed it data. 您的UITableView将需要一个数据源对象来提供数据。 If the number or rows from the datasource is more than what can fit onscreen, the table will scroll. 如果数据源中的行数或数量超出屏幕显示的容量,则表格将滚动。

If you want to programmatically scroll the table, you can use the offset property on UIScrollView or one of the methods on UITableView to do that. 如果要以编程方式滚动表,则可以使用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/UITableView_Class/Reference/Reference.html

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

As you increase Your tableview rows it automatically scrolls. 随着增加tableview行,它会自动滚动。 No need to include extra properties for tableview. 无需包括tableview的其他属性。

For example: 例如:

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

You don't make it actually scrollable by setting a property or so. 您不能通过设置某个属性来使它实际上可滚动。 Look a the UITableviewDataSource and UITableviewDelegate Protocols. 查看UITableviewDataSource和UITableviewDelegate协议。 By implementing those messages you provide data for the table view and it will then scroll automatically as needed. 通过实现这些消息,您可以为表视图提供数据,然后它将根据需要自动滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM