How can I set number Of rows in section in SUBCLASS of UITableView (not in view controller)?
is there a function , say, [self setNumberOfRowInSection:] or something like this?
I know about delegate method
- (int)tableView:numberOfRowsInSection;
BUT I'AM NOT IN VIEW CONTROLLER!
A made a class, wich is subclass of UITableView, and in some action, a want to SET (not to GET)
number of rows for section.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
this will create 5 rows in section so you can write whatever your desired output is if from array then write [myArray count]; instead of 5.
You can use Delegate& DataSource method of UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10; //no. of row you want
}
Related
I'm trying to make a UITableView present a determined number of rows for a section, but even when I verify that its data source is returning x number of rows for numberOfRowsInSection, the table view shows x-1.
The exception to this unexpected behavior is if the numberOfRowsInSection is less than 3.
I've even put a breakpoint in cellForRowAtIndexPath and I confirmed it's being called for the row that is not appearing.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == SectionNumber_One) {
return 6;
} else {
return self.numberOfProjectRows; // This returns x, but x-1 rows are being shown
}
}
For example, if self.numberOfProjectRows is 5, only 4 rows are shown for the second section.
If I increase it manually to 6, it shows 5 rows but the data that should be in the 5th position, isn't there.
It doesn't seem to be related to screen size as I tested it on an iPad with same results.
Why is this happening? Is there some other possible modifier of the number of rows in a section?
I'm attaching an screenshot if it's of any help.
EDIT - Here are my delegate methods:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Cell with reuse identifier setup
if (section == SectionNumber_One) {
// cell setup for section one that's showing up ok
} else if (section == SectionNumber_Two) {
UITextField *projectField = cell.projectTextField;
if ([self.userProjectKeys count] > row) {
projectField.text = self.availableProjects[self.userProjectKeys[row]];
}
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Hide the password row for existing users
if (indexPath.row == FieldTag_Password && ![self.user.key vol_isStringEmpty]) {
return 0.0f;
} else {
return UITableViewAutomaticDimension;
}
}
The problem is probably not in your Datasource methods, but your Delegate methods, tableView(_:heightForRowAt:).
If you do not return a correct height for the cell, it won't show up.
It doesn't matter if you write 1000 cells in your datasource. If you don't return the height, they wont show up.
You are not comforming to the MVC pattern in implementing the table. You must return the count of the tableView's datasource, not variables of it.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == SectionNumber_One) {
return 6;
} else {
return [_displayingArray count]; // <-- here
}
}
If you want different items for each sections, then declare different datasource (ie array) for each of them.
EDIT: Even returning constant 6 is dangerous in the other section - you ought to add items to another fixed array and return that array's count in this delegate.
I am trying to implement accordion tableview with parent and child custom tableview cells. I am using below mentioned open source code.
Source code : https://github.com/singhson/Expandable-Collapsable-TableView
In that code having single tableview with single tableview cell. It will show for parent and child cells but I want to make:
Main storyboard Tableview
Parenttableviewcell separate class and xib
Childtableviewcell separate class and xib
It should apply on main controller tableview with accordion. Right now in this code there is no separate custom cells (parent and child using same tableview cell and changing data only).
You can implement it by trying to manage the sections and rows of a table view, thats what i assume to be the simplest way to implement it without using any third party code. For example
All the section headers will contain their own number of rows.
Take a array or dictionary or array that will store the state of expanded rows in sections. (Say '1' for expanded state and '0; for collapsed state)
If in the initial state, all the UI is expanded then set all the objects '1' in array or dictionary.
On tap of individual headers (I am assuming section will be collapsed on click on individual header of section) you can get which section need to be collapsed. Retain this state as '0' for collapsed section rows in the array or dictionary.
Reload the table and check in heightForRow delegate method for the '0' entity in your array or dictionary. Wherever you find it to be '0', return height as 0 in the delegate method.
Perform the opposite for reverse functionality.
UPDATE FOR CODE
- (IBAction)btnMenuViewTypeTapped:(id)sender{
UIButton *btnSender = (UIButton *)sender;
if(menuViewType == MVTCollapse){
[btnSender setImage:[UIImage imageNamed:#"MenuCollapse"] forState:UIControlStateNormal];
menuViewType = MVTExpand;
for(NSInteger intAtIndex=0; intAtIndex<[mutArrSearchMenuItems count]; intAtIndex++){
[mutArrSectionOpened replaceObjectAtIndex:intAtIndex withObject:#"1"];
}
} else{
[btnSender setImage:[UIImage imageNamed:#"MenuExpand"] forState:UIControlStateNormal];
menuViewType = MVTCollapse;
for(NSInteger intAtIndex=0; intAtIndex<[mutArrSearchMenuItems count]; intAtIndex++){
[mutArrSectionOpened replaceObjectAtIndex:intAtIndex withObject:#"0"];
}
}
[tblViewRestaurantMenu reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [mutArrSearchMenuItems count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSInteger intNumOfRow;
if([mutArrSectionOpened[section] isEqualToString:#"1"]){
intNumOfRow = [mutArrSearchMenuItems[section][strMenuType] count];
}
else{
intNumOfRow = 0;
}
return intNumOfRow;
}
You can implement it by using SKSTableview.
for that Please refer the below link:
SKSTableView
Let make two kinds of custom cell.
+Parent cell is tableview sectionHeader
+Child cell is normal cell
/*your datasource like this
arrData = #[section, section, section ....]
section.name
section.image
section.arrayChild
arrayChild = #[child, child, child....]
child.name
child.image
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return arrData.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
section = arrData[section];
child = section.child;
return child.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//Cell is Custom of UITableViewHeaderFooterViewCell
//load your Parent Cell here
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//load your custom chill cell here
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(#"Test1");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
NSLog(#"No. of sections- %d",section);
return [[[BNRItemStore sharedStore] allItems] count] ;//no. of rows
}
In the above scenario, numberOfSectionsInTableView: method is being called twice before tableView method is called. I can't understand why.
The other thing that confuses me is that when I returned 1 as number of sections in numberOfSectionsInTableView: method, why does it not reflect in - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section method? It rather logs it as 0.
Doesn't the section parameter get updated since I set it as 1 previously? I mean at runtime the tableView:numberOfRowsInSection: method is called internally so obviously section parameter is supposed to hold some value.
Isn't that the case?
Number of section is the count of sections in the TableView & the index of row & section starts from 0.
Sections and rows in tableViews are just like arrays, they start with 0.
Try like this inspite of returning 1:-
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(#"Test1");
return [yourArray count];
}
I have two tableviews and cells are configured depending on which table view it is, they are implemented in the cellForRowAtIndexPath method via indexpath.section. However, the two tableviews have a different number of sections. Is there a way to go about this issue? Thanks
When i said they had different section numbers i meant the following:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if(tableView==byTeam) {
return [teamnameswithoutRepeat count];
} else {
return [JSONPointsArray count];
}
// Return the number of sections.
}
When i use the following in cellforrowatindexpath:
label.text=[currentarray objectAtIndex: indexpath.section];
there is a range error because indexpath.section is too big, it is using the JSONPointsArray to determine the number of sections, however when i run the app the by team tableview has the correct amount of sections.
Your cellForRow... method needs to be similar:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == byTeam) {
// process the "byTeam" table using the teamnamesiwthoutRepeat array
} else {
// process the other table using the JSONPointsArray array
}
}
I have two UITableView which appears according to UISegmentControl.selectedSegmentIndex. The first table is grouped style and the second is plain.
NSFetchedResultsController is for the first UITableView, and NSMutableArray for the second.
In my numberOfSectionsInTableView:
return [_fetchedResultsController.sections count];
In this case the second UITableView's data displays for 3 times (because FirstTableView section count = 3).
I would like for the second tableView numberOfSectionsInTableView
return 1;
You should check which tableview you are displaying before returning the count. You can use the tableView input param for this.
For eg:-
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView == self.firstTableView) //or (tableView == firstTableView)
return [_fetchedResultsController.sections count];
else
return 1;
}
You can use the following condition...
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
if(tableView == firstTableView)
return 1;
else if (tableView == secondTableView)
return 2;
//... and so on you can do like this....
}
All the best !!!
I guess you are setting the same data source for both table views. If that is the case, then in your:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
if(tableView == firstTableView){ //assuming you have a reference to the first table view
return [_fetchedResultsController.sections count];
}else{
return 1;
}
}
You will have to do this in all your datasource methods. Instead it would be easier to make the datasource of the tableviews different.
To do this you can drag an object (the blue cube ) from the object library and set its class as say, SecondTableDatasource. And then set this object as the datasource for your second table, and implement the corresponding datasource methods in that class.
You can add tag for each tableView and check for each tag in numberOfSectionsInTableView