I need to load the "Main Entity" that has a relationship one-to-many with "Child Entity" into the same UITableView, how can i do?
Thanks in advance.
Update
i use the following code to populate the UITableView:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[[fetchedResultsController sections] objectAtIndex:section] name];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
if (self.selectedIndexPath) {
return [sectionInfo numberOfObjects] + [self.childArray count];
}else {
return [sectionInfo numberOfObjects];
}
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
OreRecupero *selectedObj=(OreRecupero*)[self.fetchedResultsController objectAtIndexPath:indexPath];
OrarioMainCell *cell = nil;
if (indexPath == self.selectedIndexPath) {
NSUInteger giorniCount = [selectedObj.giorni count];
NSInteger row = indexPath.row;
if (indexPath.row < giorniCount) {
cell = (OrarioMainCell *)[tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell=(OrarioMainCell *)[[[NSBundle mainBundle]loadNibNamed:#"myCustomCell" owner:self options:nil]objectAtIndex:11];
radiusLabel=[[LabelRadiusAndText alloc]init];
}
UIImageView *bgImage=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"fondoCella"]]autorelease];
cell.backgroundView=bgImage;
[df setDateFormat:#"HH:mm"];
cell.textLabel.text = [NSString stringWithFormat:#"%# - %#",[df stringFromDate:selectedObj.oraInizio],[df stringFromDate:selectedObj.oraFine]];
} else {
cell = (OrarioMainCell *)[tableView dequeueReusableCellWithIdentifier:#"DetailCell"];
if (cell == nil) {
cell=(OrarioMainCell *)[[[NSBundle mainBundle]loadNibNamed:#"myCustomCell" owner:self options:nil]objectAtIndex:13];
radiusLabel=[[LabelRadiusAndText alloc]init];
}
GiornoRecupero *giorno = [childArray objectAtIndex:row];
cell.textLabel.text = [NSString stringWithFormat:#"%i", giorno.oreStraordinario];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%i", giorno.durata];
}
} else {
cell = (OrarioMainCell *)[tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell=(OrarioMainCell *)[[[NSBundle mainBundle]loadNibNamed:#"myCustomCell" owner:self options:nil]objectAtIndex:11];
radiusLabel=[[LabelRadiusAndText alloc]init];
}
UIImageView *bgImage=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"fondoCella"]]autorelease];
cell.backgroundView=bgImage;
[df setDateFormat:#"HH:mm"];
cell.orariLbl.text = [NSString stringWithFormat:#"%# - %#",[df stringFromDate:selectedObj.oraInizio],[df stringFromDate:selectedObj.oraFine]];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
OreRecupero *selectedObj=(OreRecupero*)[self.fetchedResultsController objectAtIndexPath:indexPath];
NSIndexPath *dropDownCellIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1
inSection:indexPath.section];
//if user tapped the same row twice let's start getting rid of the control cell
if (!self.selectedIndexPath) {
// Show Drop Down Cell
for (GiornoRecupero *giorno in selectedObj.giorni) {
[self.childArray addObject:giorno];
}
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:dropDownCellIndexPath]
withRowAnimation:UITableViewRowAnimationTop];
self.selectedIndexPath = indexPath;
} else {
NSIndexPath *currentdropDownCellIndexPath = [NSIndexPath indexPathForRow:self.selectedIndexPath.row + 1
inSection:self.selectedIndexPath.section];
if (indexPath.row == self.selectedIndexPath.row) {
// Hide Drop Down Cell
[self.childArray removeAllObjects];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:currentdropDownCellIndexPath]
withRowAnimation:UITableViewRowAnimationTop];
self.selectedIndexPath = nil;
} else if (indexPath.row == currentdropDownCellIndexPath.row) {
// Dropdown Cell Selected - No Action
return;
} else {
// Switch Dropdown Cell Location
[self.myTableView beginUpdates];
// Hide Dropdown Cell
[self.childArray removeAllObjects];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:currentdropDownCellIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
// Show Dropdown Cell
NSInteger dropDownCellRow = indexPath.row + ((indexPath.row >= currentdropDownCellIndexPath.row) ? 0 : 1);
dropDownCellIndexPath = [NSIndexPath indexPathForRow:dropDownCellRow
inSection:indexPath.section];
for (GiornoRecupero *giorno in selectedObj.giorni) {
[self.childArray addObject:giorno];
}
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:dropDownCellIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
self.selectedIndexPath = [NSIndexPath indexPathForRow:dropDownCellIndexPath.row - 1
inSection:dropDownCellIndexPath.section];
[self.myTableView endUpdates];
}
}
}
i would like to insert new rows using didSelectRowAtIndexPath method, getting data from "Child Entity"
Related
I want to handle multiple row selection i.e., not more than 3, from where the data is coming from a service(URL). below is some part of code i have tried, but cannot handle the multiple row selection. Could you spare some time to solve it. TIA
- (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.professionArr objectAtIndex:row];
if(_WSConstProfessionID !=nil && ![_WSConstProfessionID isEqual:#"0"] && ![_WSConstProfessionID isEqual:#""] && _WSConstProfessionSelectedIndex ==row ){
cell .accessoryType=UITableViewCellAccessoryCheckmark;
} else {
cell .accessoryType=UITableViewCellAccessoryNone;
}
cell.lblTitle.text = [thisRow objectForKey:_WSColumnProfessionName];
NSString *str=[thisRow objectForKey:_WSColumnProfessionID];
NSString *stra=_WSConstProfessionID;
if ([str isEqualToString:stra]) {
cell .accessoryType=UITableViewCellAccessoryCheckmark;
cell.highlighted=YES;
} else {
cell .accessoryType=UITableViewCellAccessoryNone;
}
return cell;
}
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSUInteger row = [indexPath row];
NSDictionary *thisRow=[self.professionArr objectAtIndex:row];
NSLog(#"%#",[thisRow description]);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
if(_WSConstProfessionID!=nil && ![_WSConstProfessionID isEqual:#"0"] && ![_WSConstProfessionID isEqual:#""] &&_WSConstProfessionSelectedIndex!=row){
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_WSConstProfessionSelectedIndex inSection:0]];
if (cell != nil){
cell .accessoryType=UITableViewCellAccessoryNone;
}
}
if( cell.accessoryType == UITableViewCellAccessoryCheckmark){
_WSConstProfessionID=[thisRow objectForKey:_WSColumnProfessionID];
_WSConstProfessionName=[thisRow objectForKey:_WSColumnProfessionName];
_WSConstProfessionSelectedIndex=row ;
} else {
_WSConstProfessionID=#"0";
_WSConstProfessionName=#"Select";
_WSConstProfessionSelectedIndex=-1 ;
}
[self.navigationController popViewControllerAnimated:YES];
}
Please set this line in viewdidload method
tableView.allowsMultipleSelectionDuringEditing = true
After add this line Apply following code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSUInteger row = [indexPath row];
NSDictionary *thisRow=[self.professionArr objectAtIndex:row];
NSLog(#"%#",[thisRow description]);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
if(_WSConstProfessionID!=nil && ![_WSConstProfessionID isEqual:#"0"] && ![_WSConstProfessionID isEqual:#""] &&_WSConstProfessionSelectedIndex!=row){
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_WSConstProfessionSelectedIndex inSection:0]];
if (cell != nil){
cell .accessoryType=UITableViewCellAccessoryNone;
}
}
if( cell.accessoryType == UITableViewCellAccessoryCheckmark){
_WSConstProfessionID=[thisRow objectForKey:_WSColumnProfessionID];
_WSConstProfessionName=[thisRow objectForKey:_WSColumnProfessionName];
_WSConstProfessionSelectedIndex=row ;
} else {
_WSConstProfessionID=#"0";
_WSConstProfessionName=#"Select";
_WSConstProfessionSelectedIndex=-1 ;
}
[self.navigationController popViewControllerAnimated:YES];
}
plz use this code
heare i take NSMutableArray array name categoryArray.
alloc this array into viewdidload().
categoryArray = [NSMutableArray array];
in categoryArray I add the _WSColumnProfessionName ids and handle the coditions.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
#try {
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;
}
}
}
cell.lblTitle.text = [thisRow objectForKey:_WSColumnProfessionName];
if (categoryArray.count == 0)
{
NSLog(#"category array is empty.");
}
else
{
NSString *s = [NSString stringWithFormat:#"%#",[[save_cat_array objectAtIndex:indexPath.row] valueForKey:#"category_id"]];
for (int p =0 ; p<categoryArray.count; p++)
{
NSString *b =[NSString stringWithFormat:#"%#",[categoryArray objectAtIndex:p]];
if ([s isEqualToString:b])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.lblTitle.textt = [[self.professionArr objectAtIndex:indexPath.row]valueForKey:_WSColumnProfessionName];
break;
}
else
{
cell.lblTitle.text = [[self.professionArr objectAtIndex:indexPath.row]valueForKey:_WSColumnProfessionName];
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
return cell;
}
#catch (NSException *exception) {
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
#try {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = ( UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
[categoryArray addObject:[[self.professionArr objectAtIndex:indexPath.row] valueForKey:_WSColumnProfessionID];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
NSString *a = [NSString stringWithFormat:#"%#",[self.professionArr objectAtIndex:indexPath.row] valueForKey:_WSColumnProfessionID]];
for (int i=0;i<categoryArray.count;i++)
{
if ([a isEqualToString:[NSString stringWithFormat:#"%#",[categoryArray objectAtIndex:i]]])
{
[categoryArray removeObjectAtIndex:i];
}
}
}
}
#catch (NSException *exception) {
}
}
you can also handle max 3 condition on base of categoryArray.count == 3
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.
Here is my custom cell code
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
ContestTableViewCell *cell = [myContestTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *myNib;
myNib =[[NSBundle mainBundle]loadNibNamed:#"ContestTableViewCell" owner:self options:nil];
cell =[myNib objectAtIndex:0];
}
if (selectedIndex==indexPath.row)
{
}
else
{
}
cell.fldExamTitleLbl.text = [fldExamTitleArray objectAtIndex:indexPath.row];
cell.fldExamStartDateLbl.text = [fldExamStartArray objectAtIndex:indexPath.row];
cell.prizeMoney.text =[prizeMoneyArray objectAtIndex:indexPath.row];
cell.firstReward.text = [firstRewardArray objectAtIndex:indexPath.row];
cell.secondReward.text = [secondRewardArray objectAtIndex:indexPath.row];
cell.thirdReward.text = [thirdRewardArray objectAtIndex:indexPath.row];
return cell;
}
And didSelectRowAtIndexPath i am writing this code
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (selectedIndex == indexPath.row)
{
selectedIndex = -1;
[myContestTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
return;
}
if (selectedIndex != -1)
{
NSIndexPath *prevPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
selectedIndex = indexPath.row;
[myContestTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevPath] withRowAnimation:UITableViewRowAnimationFade];
}
selectedIndex = indexPath.row;
[myContestTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
I am getting Output like this it is reloading the data of JSON.
I am new to iOS please help me.
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];
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