Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to scroll TableView to a specific position

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 891
    Comment on it

     

    Hi Reader’s,

     


    This blog includes the concept to scroll tableview cell automatically towards the top according to the condition provided and create index path according to the row and section. Here we will learn how to fetch row according to the required condition and usage go indexpath in scrolling the table view. First of all we will start with the storyboard means from the user interface and in the controller we will take a tableview and tableview cell. Screenshot is given below of the controller:-

     

     

     

     


    Now in the coding part we will create outlet of label and image view in the custom class of TableView cell and then use this class further to access these outlets.

     

     

    SubTableViewCell.h
    
    #import <UIKit/UIKit.h>
    
    @interface SubTableViewCell : UITableViewCell
    @property (weak, nonatomic) IBOutlet UILabel *lblDate;
    @property (weak, nonatomic) IBOutlet UIImageView *imgView;
    
    @end
    
    
    SubTableViewCell.m
    
    #import "SubTableViewCell.h"
    
    @implementation SubTableViewCell
    
    - (void)awakeFromNib {
        // Initialization code
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];
    
        // Configure the view for the selected state
    }
    
    @end

     

     


    Now after creating the outlets we will start the coding of display data in TableView. Here in this example we will provide different dates from an array and another array for the images. Here we will use a single image multiple times if someone wants to take different images we can use different images also. We will create a separate method for this task and call that function when it is required.

     

     

    ViewController.m
    
    
    #import "ViewController.h"
    
    @interface ViewController ()
    {
        NSArray *arrOfDates;
        NSArray *arrOfImages;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    -(void)viewWillAppear:(BOOL)animated{
        
        arrOfDates = [[NSArray alloc]initWithObjects:@"Sunday, June 19, 2016",@"Wednesday, June 29, 2016",@"Tuesday, June 21, 2016",@"Wednesday, June 22, 2016",@"Thrusday, June 23, 2016",@"Friday, June 24, 2016",@"Saturday, June 25, 2016",@"Sunday, June 26, 2016",@"Monday, June 27, 2016",@"Tuesday, June 28, 2016",@"Monday, June 20, 2016",@"Thrusday, June 30, 2016", nil];
        
        arrOfImages=[[NSArray alloc]initWithObjects:@"1.png",@"2.png",@"3.png",@"1.png",@"2.png",@"3.png",@"1.png",@"2.png",@"3.png",@"1.png",@"2.png",@"3.png", nil];
       
        NSIndexPath *currentItemIndexPath = [self createIndexPath];
        if (currentItemIndexPath) {
            [self performSelector:@selector(scrollToCurrentDateItem:) withObject:currentItemIndexPath afterDelay:0.5];
        }
        
    }
    
    
    - (void)scrollToCurrentDateItem:(NSIndexPath *)indexpath {
        [_tableView scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionTop animated:YES]; // scroll the tableview towards top according to the indexpath
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        
        return arrOfDates.count;
        
        
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        
        SubTableViewCell *cell;
        NSString *cellIdentifier = @"cell";
        
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        [cell.lblDate setText:[arrOfDates objectAtIndex:indexPath.row]];
        [cell.imgView setImage:[UIImage imageNamed:[arrOfImages objectAtIndex:indexPath.row]]];
        return cell;
    }
    
    
    - (NSIndexPath*)createIndexPath {
        
        NSIndexPath *index;
        
        NSString * currentDateString=[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterNoStyle]; // use to fetch the current date
        NSArray *todayDateKeyArr = [arrOfDates filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF=%@",currentDateString]];
        NSInteger section=0,row=0;
        row = [arrOfDates indexOfObject:[todayDateKeyArr firstObject]]; //fetch the index of row
       index=[NSIndexPath indexPathForRow:row inSection:section]; // create indexpath according to the section and row.
       return index;
    }
    
    @end

     

     


    Output:-

     

    Now in output TableView will automatically scroll to the specific position according to the condition provided. As here in this application we want to scroll the TableView according to the current date so TableView will scroll towards the current date automatically.

     

     

     


    You can also download the sample project from the link given below:-

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: