How to make Table view scrollable
How do I make UITableView scrollable?
When I scroll down or scroll up outside of the tableview area,application quite. I use tablehandler class for my table view data source.Here is some of code from my tablehandler class.// 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(compiledState开发者_运维技巧ment,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. It will scroll for you automagically.
Your UITableView will need a data source object to feed it data. 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.
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
As you increase Your tableview rows it automatically scrolls. No need to include extra properties for 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. By implementing those messages you provide data for the table view and it will then scroll automatically as needed.
精彩评论