1

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(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
Phoenix Kyaw
  • 332
  • 1
  • 5
  • 17
  • 1
    By default a TableView is scrollable. In case yours is not, you mak enable the option in XIB. `Scrolling Enabled = YES` – Sahil Khanna Oct 10 '11 at 05:01

3 Answers3

4

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

D.C.
  • 15,340
  • 19
  • 71
  • 102
  • 1
    In my program,When my scrolling reaches the out of range area of tableview, program quite.I don't know how to fix it.thank. – Phoenix Kyaw Oct 10 '11 at 05:24
  • Hmm sounds like a more serious problem if you are crashing. Post your code and we can take a look – D.C. Oct 10 '11 at 07:08
  • It the following link solves my problem.[http://stackoverflow.com/questions/5761518/variable-is-not-a-cfstring-error].Thank anyways. – Phoenix Kyaw Oct 14 '11 at 10:02
3

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;
}
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Prajan
  • 666
  • 1
  • 6
  • 15
1

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.

HeikoG
  • 1,793
  • 1
  • 20
  • 29