How to design custom cell row wise individually in ios? - 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

Related

How to add two table views in one UIviewcontroller

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;
}
}

Limited Selection in UITableView

i have a tableview getting dynamic data from a service. i want to select multiple rows limit is 3.. i have tried all the possible ways but can't get it out.. following is my code. Please help me out to solve this issue. Thanks in Advance
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.countriesArr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
static NSString *MyIdentifier = #"tableCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell *) currentObject;
}
}
}
NSDictionary *thisRow = [self.countriesArr objectAtIndex:row];
if(_WSConstCountryID !=nil && ![_WSConstCountryID isEqual:#"0"] && ![_WSConstCountryID isEqual:#""] && _WSConstCountrySelectedIndex ==row )
{
cell .accessoryType=UITableViewCellAccessoryCheckmark;
}
else {
cell .accessoryType=UITableViewCellAccessoryNone;
}
cell.lblTitle.text = [thisRow objectForKey:_WSColumnCountryName];
NSString *str=[thisRow objectForKey:_WSColumnCountryID];
NSString *stra=_WSConstCountryID;
if ([str isEqualToString:stra]) {
cell .accessoryType=UITableViewCellAccessoryCheckmark;
cell.highlighted=YES;
}else
{
cell .accessoryType=UITableViewCellAccessoryNone;
}
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSUInteger row = [indexPath row];
NSDictionary *thisRow=[self.countriesArr objectAtIndex:row];
NSLog(#"%#",[thisRow description]);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
if(_WSConstCountryID!=nil && ![_WSConstCountryID isEqual:#"0"] && ![_WSConstCountryID isEqual:#""] &&_WSConstCountrySelectedIndex!=row)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_WSConstCountrySelectedIndex inSection:0]];
if (cell != nil)
{
cell .accessoryType=UITableViewCellAccessoryNone;
}
}
if( cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
_WSConstCountryID=[thisRow objectForKey:_WSColumnCountryID];
_WSConstCountryName=[thisRow objectForKey:_WSColumnCountryName];
_WSConstCountrySelectedIndex=row ;
}
else
{
_WSConstCountryID=#"0";
_WSConstCountryName=#"Select";
_WSConstCountrySelectedIndex=-1 ;
}
[self.navigationController popViewControllerAnimated:YES];
}
There are two things you have to follow for this.
1.Since you want to select maximum of only three rows simultaneously,keep an array for tracking those row numbers and check whether that array has the row number in CellForRowAtIndexPath.But _WSConstCountrySelectedIndex has only the latest selected row number.
2.In didSelectRowAtIndex,update your array with the current selected row number.And make sure array does not exceed the count of 3.Similarly in didDeSelectRowAtIndex when the selected row is deselected then remove that row number in the array.
It must give you a start.
In willSelectRowAtIndexPath check the count of number of rows selected. If it is less than 3 than return index path or else return nil.
- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([tableView indexPathsForSelectedRows].count > 3) {
return nil;
}
return indexPath;
}
Hope this will help you.

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;

Insert new row below a tapped row in UITableView

I've been trying to add a new row dynamically in a UITableView when the user taps on the row just above. There seem to be 2-3 other posts around this but am somehow not able to connect / leverage the same.
Following is my code. Could someone give me an idea please where I might be going wrong?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
NSInteger rowsRequired = 0;
if (section == 0) {
rowsRequired = 1;
}
else {
rowsRequired = [[self contents] count] + 1;
}
return rowsRequired;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
CellType1 *cell1 = nil;
CellType2 *cell2 = nil;
if (indexPath.section == 0) {
cell1 = [tableView dequeueReusableCellWithIdentifier:#“CellType1”];
if (cell1 == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#“CellType1” owner:self options:nil];
cell1 = [nib objectAtIndex:0];
}
return cell1;
}
else {
cell2 = [tableView dequeueReusableCellWithIdentifier:#“CellType2”];
if (cell2 == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#“CellType2” owner:self options:nil];
cell2 = [nib objectAtIndex:0];
}
return cell2;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1) {
NSIndexPath *newPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
indexPath = newPath;
[[self contents] insertNewRow:#“My New Row”];
[[self tableView] insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
I think you have to insert the new value in the array after the current index path value
[contents insertObject:myObject atIndex:10];

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!!

Resources