How to add two table views in one UIviewcontroller - ios

I have created a page where i need to use two table views for adding dynamic options. The problem is that only one table is displaying its data and the other one is not. Am totally confused where did i do wrong.
screen shot of my views.
Below am adding my code-
#interface FlirtMatchViewController ()
{
UILabel *headerTitle;
NSArray *tableData;
NSArray *arrimages;
NSArray *tableData1;
NSArray *arrimages1;
}
#end
#implementation FlirtMatchViewController
- (void)viewDidLoad {
[super viewDidLoad];
tableData = [NSArray arrayWithObjects:#"TattooSingles",#"Eyecatcher",#"Tattoo Toplist",#"Radar",#"Flirt Chat",#"Free VIP Membership",#"VIP Membership",#"Invite Friends",nil];
arrimages = [NSArray arrayWithObjects:#"tatto_single#2x.png",#"eye_catcher.png",#"tatto_toplist.png",#"radar.png",#"Flirt_chat.png",#"free_vip.png",#"vip_membership_navigation.png",#"add-friend1.png", nil];
tableData1 = [NSArray arrayWithObjects:#"TattooSingles",#"Eyecatcher",#"Tattoo Toplist",#"Radar",#"Flirt Chat",#"Free VIP Membership",#"VIP Membership",#"Invite Friends",nil];
arrimages1 = [NSArray arrayWithObjects:#"tatto_single#2x.png",#"eye_catcher.png",#"tatto_toplist.png",#"radar.png",#"Flirt_chat.png",#"free_vip.png",#"vip_membership_navigation.png",#"add-friend1.png", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView==_leftTableView)
{
return [tableData count];
}
else {
return [tableData1 count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier2 = #"Cell2";
static NSString *CellIdentifier1 = #"Cell1";
if(tableView==self.leftTableView)
{
static NSString *CellIdentifier1 = #"Cell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] ;
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
else
{
static NSString *CellIdentifier2 = #"Cell2";
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell1 == nil)
{
cell1= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] ;
}
cell1.textLabel.text = [tableData1 objectAtIndex:indexPath.row];
return cell1;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
TattooSinglesScreen *obj=[self.storyboard instantiateViewControllerWithIdentifier:#"TattooSinglesScreen"];
EyecatcherScreen *obj1=[self.storyboard instantiateViewControllerWithIdentifier:#"EyecatcherScreen"];
TattooToplist *obj2=[self.storyboard instantiateViewControllerWithIdentifier:#"TattooToplist"];
int i=indexPath.row;
if(i==0){
[self.navigationController pushViewController:obj animated:NO];
}
else if (i==1) {
[self.navigationController pushViewController:obj1 animated:NO];
}
else if (i==2) {
[self.navigationController pushViewController:obj2 animated:NO];
}
}
Also am trying to navigate the first three options to their particular screen by using the code below. But the code is not working for me. Can anyone clear my doubts?

What Anbu suggested will work, although could I suggest you create separate classses to handle the datasource and delegate for each UITableView, then simply assign the datasource and delegate to your new classes for each tableview. This will be cleaner and might help resolve the logic issues you have.
Hope this helps, comment if you have any questions, good luck.

do like
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self. leftTableView)
{
return [tableData count];
}
if (tableView == self. rightTableView)
{
return [tableData1 count];
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
if(tableView==self.leftTableView)
{
static NSString *CellIdentifier1 = #"Cell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] ;
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
if (tableView == self._rightTableView)
{
static NSString *CellIdentifier2 = #"Cell2";
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell1 == nil)
{
cell1= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] ;
}
cell1.textLabel.text = [tableData1 objectAtIndex:indexPath.row];
return cell1;
}
}

Related

Hide Custom Cell in TableView

I have the following implementation, where I am adding a footerview as a customcell. However, if there is no content, I do not want to show the last cell, if there is I want to show it.
In my current implementation, it always displays the last cell.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.adminOrderElements count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return adminOrderElements[section].products.count + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row < adminOrderElements[indexPath.section].products.count)
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSArray * tempArray = adminOrderElements[indexPath.section].products;
cell.textLabel.text = [[tempArray objectAtIndex:indexPath.row] objectForKey:#"productname"];
return cell;
}
else
{
static NSString *CellIdentifier = #"FooterCell";
FooterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[FooterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if(adminOrderElements[indexPath.section].expanded && [adminOrderElements[indexPath.section].notes length]>0)
{
cell.footerLabel.text = [NSString stringWithFormat:#"Notes: %#", adminOrderElements[indexPath.section].notes];
}
else
{
cell.heightConstraints.constant = 1;
cell.footerLabel.text = #"";
}
return cell;
}
}
There is one option that you can make cellHeight to 0.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(CHECK_IF_NO_CONTAIN)
return 0;
return 40;// Normal height as you want
}
I think you can add condition in else clause like if certain condition is met then you want to display this cell other wise not...
if (indexPath.row < adminOrderElements[indexPath.section].products.count)
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSArray * tempArray = adminOrderElements[indexPath.section].products;
cell.textLabel.text = [[tempArray objectAtIndex:indexPath.row] objectForKey:#"productname"];
return cell;
}
else //Here add your condition here if you have content for the section like I think this condition might work [adminOrderElements[indexPath.section].notes length]>0
{
static NSString *CellIdentifier = #"FooterCell";
FooterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[FooterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if(adminOrderElements[indexPath.section].expanded && [adminOrderElements[indexPath.section].notes length]>0)
{
cell.footerLabel.text = [NSString stringWithFormat:#"Notes: %#", adminOrderElements[indexPath.section].notes];
}
else
{
cell.heightConstraints.constant = 1;
cell.footerLabel.text = #"";
}
return cell;
}
}
Its quite simple with UITableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (dataObjects.count == 0){
return 0;
}
return dataObjects.count;
}

Tatable View multiple selection not working properly?

I am trying to implement multiselection in table view. But it is not working. When I add the below code it just shows me the single section for the elements in the table view. When I remove this line from code then I am able to select but not able to deselect the items.
Removed line code:multi selection works but not able to deselect
for (i=0; i<[arr_selectedCells count]; i++) {
NSInteger currentValue = [(NSNumber *)[arr_selectedCells objectAtIndex:i] integerValue];
if (currentValue==indexPath.row) {
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
}
correct.
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
UserFriend *user_friend=[arr_userFriends objectAtIndex:indexPath.row];
cell.textLabel.text=user_friend.name;
NSLog(#"name is %#",user_friend.name);
int i;
for (i=0; i<[arr_selectedCells count]; i++)
{
NSInteger currentValue = [(NSNumber *)[arr_selectedCells objectAtIndex:i]integerValue];
if(currentValue==indexPath.row)
{
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType=UITableViewCellAccessoryNone;
}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(![arr_userFriends containsObject:[NSNumber numberWithInteger:indexPath.row]])
{
[arr_selectedCells addObject:[NSNumber numberWithInteger:indexPath.row]];
}
else
{
[arr_selectedCells removeObject:[NSNumber numberWithInteger:indexPath.row]];
}
NSLog(#"numr is %ld",(long)indexPath.row);
[tableView reloadData];
}
Please tell me where I am going wrong?
i just edited your code! and i hope it works for you
tableView CellForRowAtIndexPath
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
UserFriend *user_friend=[arr_userFriends objectAtIndex:indexPath.row];
cell.textLabel.text=user_friend.name;
if([arr_selectedCells containsObject:user_friend.name])
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
didSelectRowAtIndexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UserFriend *user_friend=[arr_userFriends objectAtIndex:indexPath.row];
if([arr_selectedCells containsObject:user_friend.name])
{
[arr_selectedCells removeObject:user_friend.name];
}
else
{
[arr_selectedCells addObject:user_friend.name];
}
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
try these
tableView.allowsSelection=YES;
tableView.allowsSelectionDuringEditing = YES;

Retaining UITableViewCell selection statuses

Why the UITableViewCells don't reload the checkmarks after selecting, scrolling away, then scrolling back?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
#define CHECK_NULL_STRING(str) ([str isKindOfClass:[NSNull class]] || !str)?#"":str
static NSString *CellIdentifier = #"inviteCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.textLabel.highlightedTextColor = [UIColor colorWithHexString:#"#669900"];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundColor = [UIColor blackColor];
cell.textLabel.textColor = [UIColor whiteColor];
[[UITableViewCell appearance] setTintColor:[UIColor colorWithHexString:#"#669900"]];
if (cell == nil) {
cell = [[UITableViewCell alloc] init];
}
if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }
BOOL isSearching = tableView != self.tableView;
NSArray *arrayToUse = (isSearching ? searchResults : contactsObjects);
id p = arrayToUse[indexPath.row];
NSString *fName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByFirstName));
NSString *lName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByLastName));
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", CHECK_NULL_STRING(fName), CHECK_NULL_STRING(lName)];
BOOL showCheckmark = [[stateArray objectAtIndex:indexPath.row] boolValue];
if (showCheckmark == YES)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSLog(#"It hit showCheckmark = YES, and stateArray is %#",stateArray[indexPath.row]);
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
NSLog(#"It hit showCheckmark = NO, and stateArray is %#",stateArray[indexPath.row]);
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
id object = contactsObjects[indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[stateArray insertObject:[NSNumber numberWithBool:YES] atIndex:indexPath.row];
[selectedObjects addObject:object];
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
[stateArray insertObject:[NSNumber numberWithBool:NO] atIndex:indexPath.row];
[selectedObjects removeObject:object];
}
//slow-motion selection animation.
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
You missed out the ! (inverse operator) on the following line meaning that the state will always be the same.
[stateArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:[[stateArray objectAtIndex:indexPath.row] boolValue]]];
It should be
[stateArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:![[stateArray objectAtIndex:indexPath.row] boolValue]]];
Edit --- I've refactored both methods because it can be done with a lot less code and it will completely simplify the methods for you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"inviteCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
BOOL isSearching = tableView != self.tableView;
NSArray *arrayToUse = (isSearching ? searchResults : contactsObjects);
id p = arrayToUse[indexPath.row];
NSString *fName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByFirstName));
NSString *lName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByLastName));
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", CHECK_NULL_STRING(fName), CHECK_NULL_STRING(lName)];
BOOL showCheckmark = [stateArray[indexPath.row] boolValue];
if (showCheckmark == YES) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
id object = contactsObjects[indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[selectedObjects addObject:object];
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[selectedObjects removeObject:object];
}
stateArray[indexPath.row] = #(cell.accessoryType == UITableViewCellAccessoryCheckmark);
}
I suggest a more object oriented approach. This will ensure that your code is flexible and displays correctly all the time.
For each item you wish to display in your table, have a corresponding object. You mentioned that you are displaying contacts, so let's suppose your object is called "Contact":
//Contact.h
#interface Contact : NSObject
#property BOOL selected;
#property NSString *name;
#end
//Contact.m
#import Contact.h
#implementation Contact
+ (id) contactWithName:(NSString*)name {
Contact *nContact = [Contact new];
nContact.name = name;
nContact.selected = NO;
return nContact;
}
#end
Then, just make your view work something like this:
//ContactView.m
#interface ContactView()
#property NSMutableArray *contacts;
#end
#implementation ContactView
#synthesize contacts;
- (void) viewDidLoad {
[super viewDidLoad];
//get your contact list here. When creating contacts, be sure to assign their selected and their name as you require.
contacts = #[[Contact contactWithName:#"John"], [Contact contactWithName:#"Jane"]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = #"inviteCell";
UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
Contact *cellContact = [contacts objectAtIndex:indexPath.row];
cell.textLabel.text = cellContact.name;
cell.accessoryType = cellContact.selected == YES ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Contact *cellContact = [contacts objectAtIndex:indexPath.row];
cellContact.selected = !cellContact.selected;
[contacts replaceObjectAtIndex:indexPath.row withObject:cellContact];
[tableView reloadData]; //to refresh without animation
//[tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [tableView numberOfSections])] withRowAnimation:UITableViewRowAnimationTop]; //to refresh with animation
}
#end
And boom, easy to use tables that always look right, queue properly, and are object oriented for easy maintenance later.
The cell selection problem was not solved, even when insertObject was replaced with replaceWithObject, however, one should not waste time setting BOOL objects with an NSInteger inside an NSMutableArray. Instead, for cell selection memory, one should use NSDictionary like this:
#property (nonatomic, strong) NSMutableDictionary * selectedRowCollection;
- (void)viewDidLoad{
self.selectedRowCollection = [[NSMutableDictionary alloc] init];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
id object = contactsObjects[indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.selectedRowCollection setObject:#"1" forKey:[NSString stringWithFormat:#"%d",indexPath.row]];
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedRowCollection removeObjectForKey:[NSString stringWithFormat:#"%d",indexPath.row]];
}
//slow-motion selection animation.
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL showCheckmark = [[self.selectedRowCollection valueForKey:[NSString stringWithFormat:#"%d",indexPath.row]] boolValue];
if (showCheckmark == YES)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
You are not doing this UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
Allocating is necessary if cell is nil. Or it cause problem while scrolling.

Select one speicific row with one checkmark using UITbaleView

Plz anyone tell me how i select the row at one time with checkmark while other row are not checkmark.I tried but in my case there is mutiple row selected with checkmark. Basically I want to save one row with checkmark when i select another row then previous row deselect and this row select with checkmark. Here is my Code
- (NSString *)getKeyForIndex:(int)index
{
return [NSString stringWithFormat:#"KEY%d",index];
}
- (BOOL) getCheckedForIndex:(int)index
{
if([[[NSUserDefaults standardUserDefaults] valueForKey:[self getKeyForIndex:index]]boolValue]==YES)
{
return YES;
}
else
{
return NO;
} }
- (void) checkedCellAtIndex:(int)index
{ BOOL boolChecked = [self getCheckedForIndex:index];
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:!boolChecked] forKey:[self getKeyForIndex:index]];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return List.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *subviewCells = #"Cells";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:subviewCells];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:subviewCells];
}
cell.textLabel.text = [List objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self checkedCellAtIndex:indexPath.row];
if([self getCheckedForIndex:indexPath.row]==YES)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
selectLanguage = [List objectAtIndex:indexPath.row];
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self checkedCellAtIndex:indexPath.row];
if([self getCheckedForIndex:indexPath.row]==NO)
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
selectLanguage = [List objectAtIndex:indexPath.row];
}
#end;
Try this :
Single Row Selection:
create a new variable to track the index In Controller:
int selectedIndex;
in UITableView cellForRowAtIndexPath method:
if(indexPath.row == selectedIndex)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
and in UITableView didSelectRowAtIndex method:
selectedIndex = indexPath.row;
[tableView reloadData];
2 way as per you want:
.h file:
NSIndexPath* checkedIndexPath;
#property (nonatomic, retain) NSIndexPath* checkedIndexPath;
.m file:
#synthesize checkedIndexPath;
- (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];
}
//do you stuff here
if([self.checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//do work for checkmark
if(self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
if([self.checkedIndexPath isEqual:indexPath])
{
self.checkedIndexPath = nil;
}
else
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;
}
}
Happy coding!!

How to design custom cell row wise individually in ios?

Could any one help me? Am working on expanding cell for the past one week Finally i can able to add sub menu in it. I designed two custom cells and using plist i added menu and sub menu to that. It is working well i added menu and sub menu. Now my problem is i want to add image and button to row 1,2,4,6 only using indexpath.row i assigned but this code assigning image and button to all rows But i only want to add to row 1,2,4,6 only ho i can do this pls some one help me???
interface MyHomeView ()
{
NSMutableArray *LeftPaneList;
NSArray *datalist;
}
#property (assign)BOOL isOpen;
#property (nonatomic,retain)NSIndexPath *selectIndex;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"LeftPaneMenuList" ofType:#"plist"];
LeftPaneList = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.isOpen=NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [LeftPaneList count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.isOpen) {
if (self.selectIndex.section == section) {
return [[[LeftPaneList objectAtIndex:section] objectForKey:#"SubMenu"] count]+1;;
}
}
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.isOpen&&self.selectIndex.section == indexPath.section&&indexPath.row!=0) {
static NSString *CellIdentifier = #"MyHomeViewCell2";
MyHomeViewCell2 *cell = (MyHomeViewCell2*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
NSArray *list = [[LeftPaneList objectAtIndex:self.selectIndex.section] objectForKey:#"SubMenu"];
cell.name.text = [list objectAtIndex:indexPath.row-1];
return cell;
}
else
{
static NSString *CellIdentifier = #"MyHomeViewCell";
MyHomeViewCell *cell = (MyHomeViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
cell.tag=indexPath.row;
NSString *name = [[LeftPaneList objectAtIndex:indexPath.section] objectForKey:#"MenuName"];
cell.MyHomeMenuLabel.text = name;
return cell;
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
if ([indexPath isEqual:self.selectIndex]) {
self.isOpen = NO;
[self didSelectCellRowFirstDo:NO nextDo:NO];
self.selectIndex = nil;
}else
{
if (!self.selectIndex) {
self.selectIndex = indexPath;
[self didSelectCellRowFirstDo:YES nextDo:NO];
}else
{
[self didSelectCellRowFirstDo:NO nextDo:YES];
}
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)didSelectCellRowFirstDo:(BOOL)firstDoInsert nextDo:(BOOL)nextDoInsert
{
self.isOpen = firstDoInsert;
[self.MyHomeTableView beginUpdates];
int section = self.selectIndex.section;
int contentCount = [[[LeftPaneList objectAtIndex:section] objectForKey:#"SubMenu"] count];
NSMutableArray* rowToInsert = [[NSMutableArray alloc] init];
for (NSUInteger i = 1; i < contentCount + 1; i++) {
NSIndexPath* indexPathToInsert = [NSIndexPath indexPathForRow:i inSection:section];
[rowToInsert addObject:indexPathToInsert];
}
if (firstDoInsert)
{ [self.MyHomeTableView insertRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop];
}
else
{
[self.MyHomeTableView deleteRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop];
}
[self.MyHomeTableView endUpdates];
if (nextDoInsert) {
self.isOpen = YES;
self.selectIndex = [self.MyHomeTableView indexPathForSelectedRow];
[self didSelectCellRowFirstDo:YES nextDo:NO];
}
if (self.isOpen) [self.MyHomeTableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 52;
}
This is the original o/p!
But i want o/p like this
some one help me??
you need to specify IndexPath.section First then you can check with IndexPath.row like Bellow Example:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if(indexPath.section==0)
{
if(indexPath.row==0)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}
else if(indexPath.row==2)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}
else if(indexPath.row==2)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}
else if(indexPath.row==4)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}
else if(indexPath.row==6)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
}
}
}
Th easy way to do this, is to set up 2 different dynamic prototype cells in the storyboard, each with its own identifier. In cellForRowAtIndexPath: dequeue the correct type of cell based on the indexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (indexPath.row = 1 || indexPath.row = 2 || indexPath.row = 4 || indexPath.row = 6){
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}else{
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
}
As you are using dequeueReusableCellWithIdentifier: method, it will just give cell at index path 2, 4, 6 etc to some other cell in tableView. Instead use array to store your cell and then retrieve it from array. And then you can use indexpath.row

Resources