Reputation: 870
I have tableview and two different custom cell created. suppose cell1 and cell2.
Now i want to add cell in tableview like following pattern
cell1
cell1
cell1
cell2
cell1
cell1
cell1
cell2
and so on, i have number of count of data that are displayed in cell1 but i don't know count if cell2 because it should be dymanic count, i mean to say after every 3 cell1 data 4th cell will be cell2
here is my code for tableview
#pragma mark - TableView Delegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section{
return [arrayRecipeCategoryNAME count];
//return 22;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath{
return 175;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if((indexPath.row % 3) == 0){
HomeTableViewCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:@"Cell2" forIndexPath:indexPath];
return cell2;
}
else{
HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.lblRecipeCategory.text = [arrayRecipeCategoryNAME objectAtIndex:indexPath.row];
cell.lblRecipeCategory.font = FONT_COMFORTAA_BOLD(28);
cell.imgRecipeCategory.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[arrayRecipeCategoryImage objectAtIndex:indexPath.row]]];
return cell;
}
}
Upvotes: 1
Views: 122
Reputation: 20274
First, the number of rows returned in numberOfRowsInSection:
must be adjusted to show the extra cells.
Then in cellForRowAtIndexPath:
, you need to adjust the array index to match the correct sequence that is to be used for your datasource arrays arrayRecipeCategoryNAME
& arrayRecipeCategoryImage
The following will work for your current design:
#define INTERVAL 4
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
NSInteger count = arrayRecipeCategoryNAME.count;
NSInteger numberOfUnmappedRows = count / INTERVAL;
NSInteger totalNumberOfRows = (count + numberOfUnmappedRows);
return totalNumberOfRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(((indexPath.row + 1) % INTERVAL) == 0) {
HomeTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell2" forIndexPath:indexPath];
return cell;
}
else {
NSInteger numberOfUnmappedCellsBeforeCurrent = indexPath.row / INTERVAL;
//Use following as the index for your datasource arrays
NSInteger translatedIndex = indexPath.row - numberOfUnmappedCellsBeforeCurrent;
HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.lblRecipeCategory.text = arrayRecipeCategoryNAME[translatedIndex];
//...
cell.imgRecipeCategory.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", arrayRecipeCategoryImage[translatedIndex]]];
return cell;
}
}
Upvotes: 1
Reputation: 1895
If it is fixed that every fourth cell would be cell2 then try using following code, It may help.
Int *row = indexPath.row + 1
if(row % 4 == 0){
HomeTableViewCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:@"Cell2" forIndexPath:indexPath];
return cell2;
}
else{
HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.lblRecipeCategory.text = [arrayRecipeCategoryNAME objectAtIndex:indexPath.row];
cell.lblRecipeCategory.font = FONT_COMFORTAA_BOLD(28);
cell.imgRecipeCategory.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[arrayRecipeCategoryImage objectAtIndex:indexPath.row]]];
return cell;
}
Upvotes: 0
Reputation: 4466
You can use it like that.
var Index = [indexpath.Row] + 1
if Index%4 == 0
{
// return cell 2
}
else{
// return cell 1
}
Upvotes: 0