Two TableView in one UITableViewController - ios

I need to use two UITableView in one UITableViewController. And I need to create two custom cell for each UITableView.
Can I possible to make this? please help me.

try this.....used two UITableView in one UIViewController...
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView==self.tableview1)
{
return tableview1RowCount;
}
else if(tableView==self.tableview2)
{
return tableview2RowCount;
}
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView==self.tableview1)
{
static NSString *CellIdentifier1 = #"tableview1_cell";
tableview1_cell *cell1 = (tableview1_cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell1 == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"tableview1_cell" owner:self options:nil];
cell1 = [nib objectAtIndex:0];
}
return cell1;
}
if (tableView==self.tableview2)
{
static NSString *CellIdentifier1 = #"tableview2_cell";
tableview2_cell *cell2 = (tableview2_cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell2 == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"tableview2_cell" owner:self options:nil];
cell2 = [nib objectAtIndex:0];
}
return cell2;
}
}
and for the method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
do same as above....separate tableviews with if condition and give actions to each...whatever u want...

Related

Multiple Custom Cells

I'm trying to get my custom .XIB Cell called "ProfileCell1" into the first cell of my table view (size 150) and then my second custom .XIB Cell called "ProfileCell2" into the second cell (size 60).
I can get "ProfileCell1" into the first row, but I can't figure out how to put the second one in, since the first will repeat when I increase the return value from 1 on the "numberOfRowsInSection".
Here is my code so far:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
and
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"ProfileCell1";
ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProfileCell1" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
return cell;
}
and
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 150;
}
So I know this code repeats my ProfileCell1, but I just can't figure out how to make it appear only once, and then start my ProfileCell2 from the second cell, and have both cells return 2 different values in the 'heightForRowAtIndexPath.
I've tried stuff like:
if (indexPath.row == 0) {
return 150;
else {
return 60;
}
but it doesn't seem to work. Cheers!
try this way
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section==0) {
return 1;// your require number of cell
}
else
{
return 1;
}
}
and
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==0) {
static NSString *identifier = #"ProfileCell1";
ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProfileCell1" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
return cell;
}
else
{
// your second cell coding here
static NSString *identifier = #"ProfileCell2";
ProfileCell2 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProfileCell2" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// your second cell code
//cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
return cell;
}
}
and increase height for cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==0) {
return 150;
}
else
{
return 60;
}
}
you can use two different cell in same UITableView using two section. first section use first cell .xib and second section use second .xib files.
You can also use two different cells in single section. Making some edit to Dharmesh Dhorajiya's Post.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==0) {
static NSString *identifier = #"ProfileCell1";
ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProfileCell1" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
return cell;
}
else
{
// your second cell coding here
static NSString *identifier = #"ProfileCell2";
ProfileCell2 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProfileCell2" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
return cell;
}
}
And use same code you are using to return height for two different cells.
if (indexPath.row == 0) {
return 150;
else {
return 60;
}
How about identifying the indexPath where you want to put ProfileCell1 and ProfileCell2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// say you want to use ProfileCell1 in first row
if(indexPath.row == 0) {
static NSString *identifier = #"ProfileCell1";
ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];
// cell properties
return cell;
} else {
static NSString *identifier = #"ProfileCell2";
ProfileCell2 *cell = (ProfileCell2 *)[tableView dequeueReusableCellWithIdentifier:identifier];
// cell properties
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
return 150;
else
return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
static NSString *identifier = #"ProfileCell1";
ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];
//bla bla bla code for ProfileCell1 here
return cell;
}
else
{
static NSString *identifier = #"ProfileCell2";
ProfileCell2 *cell = (ProfileCell2 *)[tableView dequeueReusableCellWithIdentifier:identifier];
//bla bla bla code for ProfileCell2 here
return cell;
}
}

customizing more than one cells using uitableview in each row in xib

friends,
i want to add more than one customised cell in a table view in xib file.
In detail i want to add different cells for each row in table i tried the below code but seems to be not working i can only customise one cell for all the rows in tableview code is as below i have tried two codes
code1 is as below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier;
identifier=#"tablecell";
tablecell *cell = (tablecell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"tablecell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
cell.name.text=#"gopi";
return cell;
if (indexPath.row==1)
{
NSString *identifier1;
identifier1=#"gopicell";
gopicell *cell1=(gopicell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell1==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"gopicell" owner:self options:nil];
cell1=[nib objectAtIndex:0];
}
cell1.gopilabel.text=#"sabari";
return cell1;
}
}
It displays only one customised cell so i tried this code code2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier;
if (indexPath.row==0)
{
identifier=#"tablecell";
tablecell *cell = (tablecell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"tablecell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
cell.name.text=#"gopi";
return cell;
}
else if(indexPath.row==1)
{
identifier=#"gopicell";
gopicell *cell1=(gopicell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell1==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"gopicell" owner:self options:nil];
cell1=[nib objectAtIndex:0];
}
cell1.gopilabel.text=#"sabari";
return cell1;
}
return nil;
}
i know this statement is wrong but i do not know what to return here if i have is it possible to customise more than one cell for each row
Update your code as below. Check for even and odd cell and put customize cell in Your table view.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier;
if (indexPath.row %2 == 0)
{
identifier=#"tablecell";
tablecell *cell = (tablecell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"tablecell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
cell.name.text=#"gopi";
return cell;
}
else if(indexPath.row %2 == 1)
{
identifier=#"gopicell";
gopicell *cell1=(gopicell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell1==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"gopicell" owner:self options:nil];
cell1=[nib objectAtIndex:0];
}
cell1.gopilabel.text=#"sabari";
return cell1;
}
return nil;
}

Custom cells not loading in tableview?

I'm having the strangest issue; does anyone know why the below table view isn't loading my custom cells? And yes, the datasource and delegate are connected to the view controller :) I've done this before, but all of a sudden it's like something missing.
.h
#interface MyAccountViewController : UIViewController {
IBOutlet UITableView *StorageTableView;
NSArray *StoredItems;
NSMutableData *data;
}
.m
- (int)numberOfSectionsInTableView: (UITableView *)tableview
{
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [StoredItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DoctorsTableIdentifier = #"StorageItemTableViewCell";
StorageItemTableViewCell *cell = (StorageItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"StorageItemTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSDictionary *node = [StoredItems objectAtIndex:indexPath.row];
[[cell itemName] setText:[node objectForKey:#"title"]];
[[cell itemDescrip] setText:[[[[node objectForKey:#"body"] objectAtIndex:0] objectForKey:#"raw"] objectForKey:#"value"]];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 99;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
Did you register your custom cell for use?
UINib *nib = [UINib nibWithNibName:#"StorageItemTableViewCell" bundle:nil];
[self.StorageTableView registerNib:nib forCellReuseIdentifier:#"StorageItemTableViewCell"];
One big problem is that you only setup each cell once. You need to change your code to:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DoctorsTableIdentifier = #"StorageItemTableViewCell";
StorageItemTableViewCell *cell = (StorageItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"StorageItemTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.itemName.text = [[StoredItems objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.itemDescrip.text = [[StoredItems objectAtIndex:indexPath.row] objectForKey:#"description"];
return cell;
}
You also need to make sure that [StoredItems count] is returning a non-zero value.

Memory warrning tableviewcell

I have two tableviewcell in xib file and I use both of them.
When I use one of them the memory is still 7.3MB, but when I use both memory grow very fast.
Please help me. I don't now what is wrong.
My code is below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
NSDictionary *rowData = [self.events objectAtIndex:row];
static NSString *CellIdentifierL = #"LeftCell";
static NSString *CellIdentifierR = #"RightCell";
SampleTableCell *cellLeft = [tableView dequeueReusableCellWithIdentifier:CellIdentifierL];
SampleTableCell *cellRight = [tableView dequeueReusableCellWithIdentifier:CellIdentifierR];
if (cellLeft == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"LeftTableCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SampleTableCell class]]) {
cellLeft = (SampleTableCell *)currentObject;
break;
}
}
}
if (cellRight == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RightTableCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SampleTableCell class]]) {
cellRight = (SampleTableCell *)currentObject;
break;
}
}
}
return (row%2==0)?cellRight:cellLeft;
}
You need to create only the cell you need. Your code should be like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
NSDictionary *rowData = [self.events objectAtIndex:row];
static NSString *CellIdentifierL = #"LeftCell";
static NSString *CellIdentifierR = #"RightCell";
if (cellLeft == nil) {
SampleTableCell *cellLeft = [tableView dequeueReusableCellWithIdentifier:CellIdentifierL];
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"LeftTableCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SampleTableCell class]]) {
cellLeft = (SampleTableCell *)currentObject;
break;
}
}
return cellLeft;
}
if (cellRight == nil) {
SampleTableCell *cellRight = [tableView dequeueReusableCellWithIdentifier:CellIdentifierR];
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RightTableCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SampleTableCell class]]) {
cellRight = (SampleTableCell *)currentObject;
break;
}
}
return cellRight;
}
return nil;
}
I change to this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
NSDictionary *rowData = [self.events objectAtIndex:row];
SampleTableCell *cell;
if(row%2==0){
cell = [[[NSBundle mainBundle] loadNibNamed:#"RightTableCell" owner:self options:nil]objectAtIndex:0];
}else{
cell = [[[NSBundle mainBundle] loadNibNamed:#"LeftTableCell" owner:self options:nil]objectAtIndex:0];
}
cell.labelName.text = [rowData objectForKey:#"name"];
return cell;
}
But I don't know that is good idea?
This is not a good way to do this. You are creating both cells for every row, but you only use one of them. You should also use the newer way to use a xib based cell -- that is, use registerNib:forCellWithReuseIdentifier: in viewDidLoad, to register both nibs. Do your check for odd/even rows first, and dequeue the correct cell (no need to check for nil when using the register method). You do it like this:
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:#"SampleCell1" bundle:nil] forCellReuseIdentifier:#"SampleCell1"];
[self.tableView registerNib:[UINib nibWithNibName:#"SampleCell2" bundle:nil] forCellReuseIdentifier:#"SampleCell2"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 2 == 0) {
SampleCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SampleCell1" forIndexPath:indexPath];
// populate cell with data
return cell;
}else{
SampleCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SampleCell2" forIndexPath:indexPath];
// populate cell with data
return cell;
}
}
This is all you need to do. There's no need to check for a nil cell, because dequeueReusableCellWithIdentifier:forIndexPath: is guaranteed to return a valid cell if you either register a nib, or make the cell in a storyboard (in which case you don't need to register anything).

UITableView reuse cells (stop duplicating)

This is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"simpleTable";
profileCellViewController *cell = [self.myTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"simpleTable" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//loading data
...
...
...
cell.name.text = name;
cell.time.text = time;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
I know the problem is in the if (cell==nil) part
but really don't know hoe I can solve this...
every time im back to this viewcontroller tab the cells duplicated

Resources