i have UITableView header custom like Instagram where my section acts header, in this tableview i also using paging to get the data from server.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (currentPage == 0) {
return 1;
}
if (currentPage < totalPages) {
return myObject.count+1;
}
return myObject.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 1;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection (NSInteger)section
{
myTimeline = [myObject objectAtIndex:section];
//the rest of my code to display text in UILabel cell, etc
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (cell.tag == kLoadingCellTag) {
currentPage++;
[self getTimeline];
}
}
Now the problem is, I already make sure that my paging is working and myObject array count is increase.
But when i do the scroll it crash in method viewForHeaderInSection with reason "index 10 beyond bounds [0 .. 9]'" (i set the paging in in server with 10 items per page)
I think the section in ViewForHeaderInSection not updating with numberOfSectionsInTableView method that i already setup.
So how to make the section in ViewForHeaderInSection updated?
Thanks..
Your numberOfSectionsInTableview has return myObject.count+1;, so the table view is dutifully asking for the header for the last section, which is beyond the bounds of your myObject array. You need either to remove the +1 or, if it's there for a reason, to include code in viewForHeaderInSection to handle that special case:
if (currentPage > 0 && currentPage < totalPages && section == myObject.count) {
// create whatever header view is appropriate for the final section
} else {
myTimeline = ... etc
}
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 creating a table view in which there are 10 sections, all having a header view but no cells. So, in short, my table view will display 10 header views only; there will be no cells in any section. Now when I do that there is some space between the section's header views. I want to remove that space. Is that possible? Can you provide me with some hint or work around to achieve this?
Here are the data source methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel * label = [[UILabel alloc] init];
label.backgroundColor = [UIColor grayColor];
return label;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 44.0f;
}
Here is the screenshot of the output:
I have a bit of a complex reason why I am doing it like this, that I wont be able to explain through writing. All I want to do is have no spacing in the section's header views. Thank you in advance.
Try this..
self.tableView.rowHeight = 0; // in viewdidload
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; // in viewdidload
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.01f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return <your header height>;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
return [[UIView alloc] initWithFrame:CGRectZero];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
return <your header view>;
}
Also have table seprator as none.
If you return 0 in tableView:heightForFooterInSection: than default value is returned (probably 10.0). It is tricky, but you can use CGFLOAT_MIN instead of 0 to remove footer.
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
Update:
Swift 3:
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
You can do it directly within the interface, you have to set the footer height to 1 like this :
Try with following code
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.25; // as for you need
}
you can also manage by following delegate method
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
Is short you can manage it by above two method.
The grouped style tableview has a default footer, you should also custom the footer to overwrite it
or
try the plain style.
typedef enum {
UITableViewStylePlain,
UITableViewStyleGrouped
} UITableViewStyle;
Change the Table View style from Grouped to Plain. This solved my problem.
So I can not up vote but I can post a reply
The correct answer to this way in fact
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
You need to use method heightForHeaderInSection . You can also change it depending on different sections for eg. at some sections you may need to show more distance & under some, you don't want to show gap. For such case you can use CGFLOAT_MIN which is 0.000001f. Giving you an example, how you can use different section with different header heights
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0 || section == 2)
{
return 55.0;
}
else
{
return CGFLOAT_MIN;
}
}
Hope it will help you.
Swift 5
if you use tableview with .grouped style, maybe you should set header delegate method like this:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return .leastNormalMagnitude
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return UIView()
}
Try using this :
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 0;
}
As mentioned in comments, try using this :
self.tableView.rowHeight = 0;
I have several sections in a uitableview as shown here:
How do I hide the first section header e.g. Men.
Try overriding GetHeightForHeader in UITableViewSource, and returning 0 it's the first section
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0) return 0;
return tableView.sectionHeaderHeight;
}
In Xamarin, it would look like:
public override float GetHeightForHeader (UITableView tableView, int section)
{
if (section == 0)
return 0;
return tableView.SectionHeaderHeight;
}
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 need to create a grouped uitableview that includes some sections and possibly different cell types in each sections.
I am trying to create something like old foursquare app, user page (includes 'leaderboard', 'friend suggestions', 'friends', 'stats', 'most explored categories' ... sections).
I am fairly new to ios programming, so that view may not be a grouped uitableview.
What I especially stuck is creating different cells for sections, and finding out which cells are clicked.
My data source will be 2 different NSArray* that consists of different data types, that's why I need different custom cells.
Since you have two different sets of data and you need to display both in different sections, you have to split the data source methods into two.
Basically, choose which dataset you want to be first and off you go.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section)return secondArray.count;
//Essentially, if statements evaluate TRUE and move forward if the inside is 1 or greater (TRUE == 1)
return firstArray.count;
//If the first if statement return hits, then the code will never reach this statement which turns this into a lighter if else statement
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section)
{
//do stuff with second array and choose cell type x
}
else
{
//do stuff with first array and choose cell type y
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get the cell with: UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(indexPath.section)
{
//perform action for second dataset
}
else
{
//perform action for first dataset
}
}
For headers, you can use either of these methods and just keep the same type of styling as above:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
You can create multiple custom subclasses of UITableViewCell, and in the tableView:cellForRowAtIndexPath: method for your UITableViewDataSource, you can use if-statements to determine what type of cell to use.
For example, here's a rough outline of what I might do:
-(UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//First, determine what type of object we're showing
if (indexPath.section == 0) {
//Create and return this cell.
} else if (indexPath.section == 1) {
//Create and return this cell.
}...
}
Here's how you'd implement numberOfRowsInSection:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return [firstSectionArray count];
} else if (section == 1) {
return [secondSectionArray count];
} ...
}
For didSelectRowAtIndexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
ObjectSelected *objectSelected = [firstArray objectAtIndex:indexPath.row];
//Now you've got the object, so push a view controller:
DetailViewController *dvc = [[DetailViewController alloc] init];
dvc.objectSelected = objectSelected;
[self.navigationController pushViewController:dvc];
} else if (indexPath.section == 1) {
//Same thing, just call [secondArray objectAtIndex:indexPath.row] instead!
}
}