Table view is one of the most frequent iOS app UI element. In iOS many of the apps utilize tableview to exhibit a list of data. Special features of tableView include headers, footers, rows, and section. It provides a list of options further selecting on it we can get more information related to the selection on the TableView.
TableView uses UITableViewCell to display the rows in the table..
TableView consists of number of cells in which the content to be shown is displayed . The cells can be scrolled in the vertical direction..
Thus we can say that tableView is a versatile interface for managing and interacting with collections of data.
Here is an example of using UITableView showing images and titleLabel..
In the storyboard drag a TableView from the object gallery and place it on the ViewController. After that drag a TableViewCell and place it on the UITableView. Select the cell of the table and give its identifier.
Now make a new file which is a subclass of UITableViewCell and assign that class to the cell.. Here that cell class is DemoTableViewCell.
In the cell class make two outlets of UIImageView and a UILabel and connect them to the UIImageView and UILabel dragged in the cell in MainStoryboard :-
@property (weak, nonatomic) IBOutlet UIImageView *imgView; // outlet of UIImageView
@property (weak, nonatomic) IBOutlet UILabel *lblMessage; // outlet of UILabel
Now in your ViewController.m file implement the following code :-
#import "ViewController.h"
#import DemoTableViewCell.h" // import the Cell Class
@interface ViewController ()
{
NSArray *arrofText; // array of text
NSArray *arrOfImages; // array of images
}
@end
@implementation ViewController
-(void)viewWillAppear:(BOOL)animated{
arrofText = [[NSArray alloc]initWithObjects:@"star",@"cross",@"rose1",@"cross", nil];
arrOfImages=[[NSArray alloc]initWithObjects:@"index.png",@"index.png",@"index.png",@"cross.png", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return arrofText.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = @"UseOfTable";
NewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
[cell.lblMessage setText:[arrofText objectAtIndex:indexPath.row]]; // set text in the label from the array of text
[cell.imgView setImage:[UIImage imageNamed:[arrOfImages objectAtIndex:indexPath.row]]]; // // set image in the label from the array of images
return cell;
}
@end
After implementing all this when you build your project it will not display anything.. Reason is the DELEGATE and DATASOURCE of tableView is not set..
For displaying the content in the TableView the tableView should have a Datasource and for managing the appearance and behavior it should have Delegate.
Set the delegate and datasource and then it’s done !!
0 Comment(s)