Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a UITableView that contain 2 different custom cell. One is ExerciseTime and one is RestTime. I want my RestTime cell to be inserted between every 2 ExerciseTime cell :
Here is my code:
-Height for row
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// RestTime
if (indexPath.row % 2 == 1) {
return 40.0f;
}
// ExerciseTime
else {
return 65.0f;
}
}
-Number of row in section (i couldnt find a way to set number of row for both RestTime and ExerciseTime)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return (self.preset.blocks.count * 2) - 1;
}
-Cell for row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row % 2 == 1) {
RestTimeTableViewCell *restTimeCell = (RestTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:RestTimeTableViewCellIdentifier forIndexPath:indexPath];
RestTime *restTime = (RestTime *)[self.restTimeArray objectAtIndex:indexPath.row];
//CustomCell
return restTimeCell;
}else{
ExerciseTimeTableViewCell *exerciseTimecell = (ExerciseTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ExerciseTimeTableViewCellIdentifier forIndexPath:indexPath];
ExerciseTime *exerciseTime = [self.exerciseTimeArray objectAtIndex:indexPath.row];
//CustomCell
return exerciseTimecell;
}
return nil;
}
How can I create a tableView with 2 custom cell like this?
First return count as total of two array in numberOfRowsInSection and then in cellForRowAtIndexPath try some thing like this.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// RestTime
if (indexPath.row % 3 == 0) {
return 40.0f;
}
// ExerciseTime
else {
return 65.0f;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (indexPath.row % 3 == 0) {
RestTimeTableViewCell *restTimeCell = (RestTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:RestTimeTableViewCellIdentifier forIndexPath:indexPath];
//Access you object from array like this
NSInteger index = (NSInteger) (indexPath.row / 3);
RestTime *restTime = (RestTime *)[self.restTimeArray objectAtIndex:index];
}
else {
ExerciseTimeTableViewCell *exerciseTimecell = (ExerciseTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ExerciseTimeTableViewCellIdentifier forIndexPath:indexPath];
//Access you object from array like this
NSInteger index = (indexPath.row - (NSInteger) (indexPath.row / 3));
ExerciseTime *exerciseTime = [self.exerciseTimeArray objectAtIndex:(index - 1)];
}
return cell;
}
Output: I have try this by setting background color as Red color for RestTimeTableViewCell and Green color for ExerciseTimeTableViewCell.
if you have n exerciseTime, you will be able to insert n-1 restTimes. So
Try this...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.exerciseTime.count + (self.restTime.count >= self.exerciseTime.count) ? self.exerciseTime.count-1 : self.restTime.count;
}
Now, add exerciseTime in even position, and add restTime in odd position until current index exists in both of those lists. if current index exceeds restTime, then continue adding exerciseTime. Let me know if that works.
Related
I have an issue while displaying BannerAds for 1st and every 5th row. While displaying data, first row is replaced with banner ads and every 5th row data is replaced with banner ads...How to overcome this. Following is what i have tried.TIA
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger n;
n= [array count];
return n;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 5 == 0) {
//configure ad cell
for(UIView* view in cell.contentView.subviews) {
if([view isKindOfClass:[GADBannerView class]]) {
[view removeFromSuperview];
}
}
else
{
Title.text=[NSString stringWithFormat:#"%# ",[dict objectForKey:#"Name"]];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 5 == 0)
return 60;
else
return 153;
}
You need to increase the number of cells you return in numberOfRowsInSection and account for the added rows in cellForRowAt
The number of advertisements will be 1 + n/5 (The first row and then every 5th row), so the number of cells in your table will be n + n/5 + 1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger n;
n= [array count];
return n/5 + n + 1;
}
Now, some of the cells you need to return from cellForRowAt will be ads and you will need to account for this when accessing your data array. The index you need is the row number - the number of advertisement rows that have come before it. This is index/5 + 1 (The first row and every 5 rows).
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 5 == 0) {
AdCell *cell = (AdCell *)[tableView dequeueReusableCellWithIdentifier:"Ad" forIndexPath: indexPath];
...
NSLog(#"Showing an ad at row %ld",indexPath.row);
return cell;
else
{
NSInteger index = indexPath.row - indexPath.row/5 - 1;
NSDictionary *dict = myArray[index];
NormalCell *cell = (NormalCell *)[tableView dequeueReusableCellWithIdentifier:"Normal" forIndexPath: indexPath];
cell.title.text=[NSString stringWithFormat:#"%# ",dict["Name"]];
NSLog(#"Showing a normal row at row %ld (data from element %ld of array)",indexPath.row,index);
return cell;
}
}
I want UITableView always has the text "No more items" in the last cell, I want this row appears regardless of number of items, include 0 items. How could I do?
You can get the total row of a section by using numberOfRowsInSection: method.
NSInteger total = [self.tableView numberOfRowsInSection:indexPath.section];
// Check if last cell or no cell.
if(indexPath.row == totalRow -1 || indexPath.row == 0)
{
// Do your operation here
}
You can perform this in your cellForRowAtIndexPath or willDisplayCell methods. Hope it helps.
Just add the row regardless of your other items:
- (void)buildMenu {
[_menuItems removeAllObjects];
// ... add your items or not if you have 0
YourCellItem *lastItem = [[YourCellItem alloc] initWithTitle:#"No more items"];
[_menuItems addObject:lastItem];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _menuItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"yourCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
YourTableViewCell *basicCell = (YourTableViewCell *)cell;
YourCellItem *item = [_menuItems objectAtIndex:indexPath.row];
basicCell.itemTitle.text = item.title;
return cell;
}
This question already has answers here:
UITableView: Handle cell selection in a mixed cell table view static and dynamic cells
(6 answers)
Closed 7 years ago.
I'm using a UITableView to show sth,and the information in section 1,section 2 will not change,but section 3 is dynamic.
Is there any way to show section 1&2 using static cell,but I can write the datasource of section3?
Here you will get as per your requirement.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0 || section == 1) {
return 1; //However many static cells you want
} else {
return [_yourArray count];
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
if (indexPath.section == 0 || indexPath.section == 1 ) {
NSString *cellIdentifier = #"staticCellType";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = #"some static content";
return cell;
} else if (indexPath.section == 1){
NSString *cellIdentifier = #"dynamicCellType";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [_yourArray objectAtIndex:indexPath.row];
return cell;
}
return nil;
}
You can easily do like this....
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3; //number of section
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch(section){
case 0:
return 1; //static value as per your requirement
break;
case 1:
return 1; //static value as per your requirement
break;
case 2:
return [yourArray count]; //static value as per your requirement
break;
default
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CELL";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (indexPath.section == 2) {
NSDictionary *dict = [yourArray objectAtIndex:indexPath.row];
}
//If you perform any task for other section then you can write in switch case
return cell;
}
I have a view that has 2 independent cells. Everything is working properly, except the numberOfRowsInSection return that must be different for each cell.
I want my cell "dummy" to only return 1 or 2.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//I need to add some code in here that will say that
//if my cell = dummy then return 1 else
//Returning table count based on number of Team Names
return _teamNames.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0 && indexPath.row == 0) {
return indexPath.row + 20 - indexPath.row;
}
if (indexPath.section == 0 && indexPath.row == 18) {
return indexPath.row + 50 - indexPath.row;
} else {
return indexPath.row + 26 - indexPath.row;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *dummycell = #"dummy";
dummytest *cellar = [tableView dequeueReusableCellWithIdentifier:dummycell
forIndexPath:indexPath];
cellar.righthere.text = #"hello";
static NSString *CellIdentifier = #"StandingsIdent";
StandingsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
Any ideas? Thank you.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// I need to add some code in here that will say that if my cell = dummy then return 1 else
// You need to set this bool value in your code somewhere - where you have some sort of toggle button between - Dummycell and StandingsViewCell
if(isDummyCell)
{
return 1;
}
else
{
return _teamNames.count; // Returning table count based on number of Team Names
}
}
You can use a NSMutableDictionary as source for your Table View data (i.e. "MyData"), so you can control the behaviour of every cell directly by the data source.
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
//list of elements
if (self.keys.count == 0) {
return 0;
}
NSString *key = [self.keys objectAtIndex:section];
NSArray *mySection = [MyData objectForKey:key];
return [mySection count];
}
where keys is the array that holds your sections. When you load your data, if you want an alphabetical order, you've only to do:
NSMutableArray *keyArray = [[NSMutableArray alloc] init];
[keyArray addObjectsFromArray:[[self.MyData allKeys] sortedArrayUsingSelector:#selector(compare:)]];
self.keys = keyArray;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Before
After
basically my point is I have customTableViewCell which has button on the right most (same as image1 "show version history"). When I clicked that I want to add a dynamic number of subviewCell which is under customTableViewCells.
my question is, how is this being implemented? is there any library that you can refer on this?
I hope that You haven't search for it :
Let's take a look on these links :
UITableViewCell expand on click
gitHub
Expand / Collapse UITableViewCell with different cell and data source
Expand collapse UITableViewCell
GitHub
Accordion table cell - How to dynamically expand/contract uitableviewcell?
Have a look on these table view implementations
https://www.cocoacontrols.com/controls/jkexpandtableview
https://www.cocoacontrols.com/controls/sdnestedtable
https://www.cocoacontrols.com/controls/uiexpandabletableview
https://www.cocoacontrols.com/controls/ocborghettiview
That's very easy.
You want to track if the section is expanded. Use a instance variable (or property) for this
if it is not expanded you return 1 in tableView:numberOfRowsInSection: for the expandable section
if it is expanded you return 1 + the number of expanded cells in t:numberOfRowsInSection:
in tableView:didSelectRowAtIndexPath: you toggle the expansion state when the first cell is tapped and insert or delete the expanded rows.
e.g.:
#interface MBMasterViewController () {
BOOL firstSectionExpanded;
}
#end
#implementation MBMasterViewController
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger count = 1;
if (section == 0 && firstSectionExpanded) { count = 3; }
return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
if (indexPath.row == 0) {
cell.textLabel.text = [NSString stringWithFormat:#"Section %ld", (long)indexPath.section];
}
else {
cell.textLabel.text = [NSString stringWithFormat:#"Sub Cell %ld", (long)indexPath.row];
}
return cell;
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row != 0) {
// don't select sub cells
return nil;
}
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *cells = #[ [NSIndexPath indexPathForRow:1 inSection:indexPath.section],
[NSIndexPath indexPathForRow:2 inSection:indexPath.section]];
NSArray *cellsToDelete;
NSArray *cellsToInsert;
if (indexPath.section == 0) {
if (firstSectionExpanded) {
cellsToDelete = cells;
}
else {
cellsToInsert = cells;
}
firstSectionExpanded = !firstSectionExpanded;
}
if (cellsToDelete) {
[tableView deleteRowsAtIndexPaths:cellsToDelete withRowAnimation:UITableViewRowAnimationAutomatic];
}
if (cellsToInsert) {
[tableView insertRowsAtIndexPaths:cellsToInsert withRowAnimation:UITableViewRowAnimationAutomatic];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}