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.
Related
I am storing at most only 3 items that are allowed to be checked at anytime.
I store the selected rows in an NSMutabeDictionary called selectedRowDictionary in didSelectRowAtIndexPath
Then in my cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"identifer" forIndexPath:indexPath];
if (_selectedRowDictionary && [_selectedRowDictionary count]) {
for (NSString *rowSelected in _selectedRowDictionary) {
BOOL isRowSelected = [[_selectedRowDictionary valueForKey:rowSelected] integerValue];
if (isRowSelected){
NSLog(#"rowSelected: %#", rowSelected);
} else {
NSLog(#"rowNotSelected: %#", rowSelected);
}
int rowIndexSelected = [[rowSelected substringFromIndex:[rowSelected length] - 1 ] integerValue];
if (isRowSelected && rowIndexSelected == indexPath.row) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
return cell;
}
-
- didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
if (_selectedRowDictionary) {
[_selectedRowDictionary removeObjectForKey:[NSString stringWithFormat:#"row%d", indexPath.row]];
NSLog(#"row %d removed from array", indexPath.row);
}
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[_selectedRowDictionary setValue:[NSNumber numberWithBool:YES] forKey:[NSString stringWithFormat:#"row%d", indexPath.row]];
}
if ([_selectedRowDictionary count] > 3) {
UITableViewCell *lastSelectedCell = [tableView cellForRowAtIndexPath:indexPath];
lastSelectedCell.accessoryType = UITableViewCellAccessoryNone;
[_selectedRowDictionary removeObjectForKey:[NSString stringWithFormat:#"row%d", indexPath.row]];
NSLog(#"row selected > 3, row%d not selected", indexPath.row);
}
}
Am I missing something that is causing the cell to be deselected again when I scroll down the tableview?
When I NSLog the dictionary, it says that those rows exist and have been selected
I have checked similar questions but, I thought I solved the cell recycling issue already.
Are you at liberty to use a mutable array instead?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"identifer" forIndexPath:indexPath];
if ([_selectedRowArray containsObject:indexPath])
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([_selectedRowArray containsObject:indexPath]) {
[_selectedRowArray removeObject:indexPath];
} else {
if (_selectedRowArray.count < 3)
[_selectedRowArray addObject:indexPath];
else {
// Don't select it
}
}
[tableView reloadData];
}
I am editing your code, try this code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"identifer" forIndexPath:indexPath];
BOOL isRowSelected = [[_selectedRowDictionary valueForKey:#(indexPath.row)] boolValue]
if (isRowSelected) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
Code changes in --didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
if (_selectedRowDictionary) {
[_selectedRowDictionary removeObjectForKey:#(indexPath.row)];
NSLog(#"row %d removed from array", indexPath.row);
}
} else {
if (_selectedRowDictionary.count == 3) {
// Don't allow for Selection
return;
}
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[_selectedRowDictionary setValue:#(YES) forKey:#(indexPath.row)];
}
}
User has to unselect the previous selected row to select new row. I edited your code in optimised way, I hope this code works for you.
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];
I want to divide the cells in groups as per condition but i am getting repeated values where i am wrong.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
{
return 1;
}
else if(section == 1)
{
return 6;
}
else if(section ==2)
{
return 2;
}
else
{
return [arrControls count]-9;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [arrControls objectAtIndex:indexPath.row];
return cell;
}
Use an array of NSMutableArrays for each section.
NSMutableArray *arrays[5];
for (int i = 0; i < 5;i++)
{
arrays[i] = [[NSMutableArray alloc]init];
}
[arrays[0] addObject:#"add Your objects in section 0"];
[arrays[1] addObject:#"add Your objects in section 1"];
[arrays[2] addObject:#"add Your objects in section 2"];
etc..
Then.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
array[section].count
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [array[indexPath.section] objectAtIndex:indexPath.row];
return cell;
}
inside your cellForRowAtIndexPath , u need to return different cells for different section
Let say u have the 3 custom classes( with inferface for all of them ) inherited from UITableViewCell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(inderPath.Section ==0 )
{
//assuing that custom class name is detailTableViewCell.h and interface file name is same .
static NSString *simpleTableIdentifier = #detailTableViewCell ;
detailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]
if(cell == nil )
{
NSArray *nib= [[NSBundle mainBundle] loadNibNamed:#"detailTableViewCell" ownder self options:nil];
cell = [nib objectAtIndex:0]
}
return cell ;
}
if(inderPath.Section ==1 )
{
similarly for another subclass of UItableViewCell ;
}
if(inderPath.Section ==2 )
{
similarly for another subclass of UItableViewCell ;
}
// one common cell for other sections
NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [arrControls objectAtIndex:indexPath.row];
return cell;
}
try this`-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(indexPath.section == 0)
{
cell.textLabel.text = [firstArray objectAtIndex:indexPath.row];
}
else if (indexPath.section == 1)
{
cell.textLabel.text = [secondArray objectAtIndex:indexPath.row];
}
else if (indexPath.section == 2)
{
cell.textLabel.text = [thirdArray objectAtIndex:indexPath.row];
}
else if (indexPath.section == 3)
{
cell.textLabel.text = [thirdArray objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = [FourthArray objectAtIndex:indexPath.row];
}
return cell;
}
`
#Dalvik. You need different array for each section. And you already have only one mutableArray.
You can take the array where each object in array will give you array for the rows.
The format of data you can use is as follows
(
{key : Array},
{key : Array},
{key : Array},
{key : Array},
)
or simply
(
(Array),
(Array),
)
inside, numberOfRowsInSection: you can access it using
{
return [[[dataArray objectAtIndex:section]ObjectFoyKey:key]count];
or
return [[dataarray objectAtIndex:section]count];
}
inside, cellFOrRowAtIndexPath: you can access it using
{
array = [[dataArray objectAtIndex:section]ObjectFoyKey:key];
cell.textlabel.text = [array objectAtIndex:indexPath.row];
or
array = [dataArray objectAtIndex:section];
cell.textlabel.text = [array objectAtIndex:indexPath.row];
}
Hope this will help you..
Please elaborate your question a little bit. Its hard to understand what exactly your problem is.
In case you are using xib or storyboard for tableCell use this approach 1:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCellIdentifier";
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = (MyCell *)[nib objectAtIndex:0];
}
In case you are not using storyboard for cell try this approach 2:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCellIdentifier";
UITableViewCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setNeedsDisplay];
}
Hope this will help you..
Here is my problem I had a table view which consists of 3 three labels in it (test1, test2, test3) which comes from webservice. I want to load all the three labels in a single table view. Here is my code below :
for (NSDictionary *entry in entries) {
projectNames = [entries valueForKey:#"NM_PROJECT"];
taskNames = [entries valueForKey:#"TASk_NAME"];
subtaskNames = [entries valueForKey:#"SUBTASK_NAME"];
}
NSLog(#"project : %#", projectNames);
NSLog(#"taskNames : %#", taskNames);
NSLog(#"subtask : %#", subtaskNames);
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [projectNames count];
}
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identitifier = #"Cell";
DLPTSTableViewCell * cell = [tableView
dequeueReusableCellWithIdentifier:identitifier
forIndexPath:indexPath];
long row = [indexPath row];
cell.textLabel.text = projectNames[row];
cell.textLabel.text = taskNames[row];
cell.textLabel.text = subtaskNames[row];
return cell;
}
I want to load the test 1 in projectnames array, test 2 in tasknames array and test 3 in subtasks arrays..
Please help me out.
If you are creating custom UITableViewCell with name "DLPTSTableViewCell" then first create three labels i.e yourLabel1, yourLabel2, yourLabel3 and use this below code :
-(DLPTSTableViewCell *)getNewCell
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"DLPTSTableViewCell" owner:nil options:nil];
DLPTSTableViewCell *cell;
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[DLPTSTableViewCell class]])
{
cell= (DLPTSTableViewCell *)currentObject;
return cell;
}
}
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCell";
DLPTSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [self getNewCell];
}
// for displaying values
cell.yourLabel1.text = your first value;
cell.yourLabel2.text = your second value;
cell.yourLabel3.text = your third value;
return cell;
}
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