Delete tableview section header view - iphone

I have a tableview with 3 sections / 3 customs section header view...
After I delete a row in one of this section and if the section come empty, how I can delete the section header view.
Thanks

One of the approaches could be to maintain flags as to whether the section has become empty and return zero height for that section's header and footer and then execute reloadSections:withRowAnimation: when the only row in section is deleted.
This is certainly better when you have section specific customizations which would become tricky to handle if we were to remove the section from our model. If there are no customizations as such, you could go about maintaining an array of arrays. Once the row array is emptied, you can discard the section from the sections array. This will reflect on reloadData.
In either case, you will need to affect the model to change the view.

what you can do after you delete the row is call the method -(void)reloadData. If you are already doing this, you need to update your datasource to indicate that the row has been deleted and in - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView you need to check that. Let me know if that helps!

One way I have dealt with this problem is implementing the section title like such:
-(NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return ([[allData objectForKey:[keys objectAtIndex:section]] count] > 0) ? [keys objectAtIndex:section]:#"";
}
This just checks that the array of objects for that section actually has something if it does it returns the name of the section if not it returns an empty string which hides the header title.
The table view does need to be updated for the change to come into effect, also I was a dictionary full of arrays and an array full of the dictionary keys. but something similar could be easily implemented. This method allows for insertion back into that section without having to re-instantiate an array.
Hope it helps.

Related

How to make UITableView footer for section not sticky [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
TableView Footer is scrolling with the table
I want to have a footer for each section that will not be sticky and would scroll with the table
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
This method defines a view for the footer that is "sticky".
Thank you in advance
Assuming you have a means by which to know when you're at the end of the data set for a particular section, why don't you just tag on a custom UITableViewCell that appears as the section footer you want? It's very simple to write the logic within tableView:cellForRowAtIndexPath: that would check if the cell being requested is within the data set for that particular section. As soon as the indexPath.row being requested is 1 greater than the available data (generally contained in an array, so if indexPath.row is equal to [array count]), return your "footer" cell. This will scroll along with the table as it scrolls, since it's just another cell. You can make it look as differently from the regular cells as you'd like.
Additionally, you would need to tell your table view that each section will have one extra row, so if you're saying something like
return [array count];
You would need to say, instead,
return [array count] + 1;
in your numberOfRowsInSection method.

Non-Alphabetical Index for UITableView

How does one implement a NON alphabetical index for a UITableView? An example of this can be seen in Apple's Remote App. When looking at songs, there is a dotted index on the right hand side that allows the user to scroll quickly through the entire table. There are also no section headers.
Thanks
I'd guess that it works by returning an array full of the “middle dot” character (Unicode 00B7), bookended by the “bullet” character (2022). In the Remote app, the index has 23 items in it; therefore, to do this, you need to split up your data into 23 sections of roughly equal length. As occulus says, returning nil or #"" in -tableView:titleForHeaderInSection: will cause no section headers to be shown.
Return the aforementioned array of characters in your data source’s -sectionIndexTitlesForTableView: method, then implement -tableView:sectionForSectionIndexTitle:atIndex: to return the section index it gives you, and you should get identical behavior to Remote’s.
If you specify #"" for a section title, the section header won't be shown, which will give you the effect you desire. To use table indexing (the right hand side tiny list), you should still organise your table in sections so it knows where to jump when a index is touched, you just have to hide the section titles as I described.
To add index items, see UITableViewDelegate's method:
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

Add blank cell at end of UITable

I'm trying to add an additional cell for custom content as the last cell in a tableView, without altering the dictionary that creates the other content of the table. I think the place to add it is in cellForRowAtIndexPath rather than adding one to the numberOfRowsInSection, which just crashes if I do.
I get a cellCount from the dictionary that creates the data for the table, then in cellForRowAtIndexPath, I have:
if (indexPath.row == cellCount) {
... stuff goes here
return cell;
}
of course, this never gets called. If I do if (indexPath.row == cellCount -1) it overwrites the last cell with content.
I can get this to work if I add a blank entry into the xml from which I am populating the dictionary, but that's ugly.
Example code would be neat!
The problem here is that tableviews are designed to easily and accurately display the contents of you data-model and you've decided you don't want to do that. You're fighting the API.
The straight forward way to do this would be to put a check in numberOfRowsInSection such that it adds one to the row count when you want to display the input row. Then in cellForRowAtIndexPath: you will need to check if the table view is asking for the input row and return the appropriate type of cell.
From a UI design point, you might want to consider whether this setup is the best. This isn't a standard setup. Is the user going to understand that they can only edit the last row of the table? Will they understand they can't edit the other rows? Does anything in the UI tell them how all this works? Does the user have to scroll to the end of the table every time they want to add data? How long can this table grow? How will displaying a keyboard for the last row of the table affect how table scrolls?
I think it would be a better design to use a footer view to display the text field such that is is visually distinct from the rest of the table. It would be programmatically simpler as well. You wouldn't have to check if the table was asking for the last row every single time it ask for a cell.
Edit:
In thinking about it, perhaps a sectioned table would be simpler. Just put the special row in its own section (with or without a header.) That would simplify you handling of the rows that source from the dictionary because the row count in that section would always be the count of the dictionary. Likewise, you could just check the section attribute of the indexpath to know what cell to return for what row.
First you need to modify the numberOfRowsInSection to return +1. Then in cellForRowAtIndexPath you need to add that extra blank cell.
You need to provide for the extra cell in both cellForRowAtIndexPath and numberOfRowsInSection.
Assuming that cellCount is the actual number of cells in your array then: (a) in cellForRowAtIndexPath return the extra custom cell when indexPath.row == cellCount, and (b) in numberOfRowsInSection you need to return cellCount+1.
Assuming a single section, an example would go something like this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [journalArray count] + 1; // add 1 for summary
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row < [journalArray count]) {
return [journalArray objectAtIndex:indexPath.row];
} else {
return summaryCell;
}
}

How do I get a Row count from a UITableView

I have a tableview I'd like to customize based on how many rows it has.
If it has no rows, I'd like the background image to prompt the user to add content.
If it has 1 or more rows, I'd like it to have a different background image, in order to display the content.
I'm using a fetched results controller to populate my tableview, by the way.
Any ideas?
Well this is generally very easy to accomplish as you need only to have UITableView properly delegated to your ViewController with appropriate delegate methods included in your .m file.
Then you can anywhere get row count like this:
[tablePropertyName numberOfRowsInSection:SECTION_NUMBER];
where section number is 0 for first section, 1 for second, etc.
In Swift, you can get the UITableView rows count by
yourTableViewName.numberOfRows(inSection: Int)
example:
yourTableViewName.numberOfRows(inSection: 0) // returns rows count in section 0
I agree with Sixten Otto's answer.
[myFetchedResultsController.fetchedObjects count];
However there's more. Above line would return the number of objects regardless the sections. However if you would want to perform this on a table with multiple sections, you would have to call it the following way.
[[myFetchedResultsController.sections objectAtIndex:<section>] numberOfObjects];
You can get the objects for the section like this.
[[myFetchedResultsController.sections objectAtIndex:<section>] objects];
** You have to replace with the number representing the section.
Hope this helps.
I'd recommend taking a look at the documentation for NSFetchedResultsController. It has example code for implementing the methods of UITableViewDataSource (including the ones that say how many sections the table has, and how many rows in each section), as well as documenting the property fetchedObjects, which you could use to see how many raw results you fetched.
[myFetchedResultsController.fetchedObjects count];
tableView.numberOfRows(inSection:) returns numbers of row in section, this method may not trigger fetched results controller's core data query.
let numberOfItems = (0..<tableView.numberOfSections).reduce(into: 0) { partialResult, sectionIndex in
partialResult += tableView.numberOfRows(inSection: sectionIndex)
}

Indexpath.row value not updating

in iphone application.
I'm trying to get indexPath.row value (out of didselectedrowatindexpath method) to do something on the basis of row selected in programmatically created tableview.
i need to access indexpath.row out of didselectedrowatindexpath method where if/else will define the action on the basis of indexpath.row.
there are 7 cards images in application and one [menu list]table view. whenever user will click on row of table view,then need to touch the image
I'm trying this code to get the IndexPath.row value. The problem is indexPath.row value is not updating everytime. It's just taking the old value. Please sugggest how to solve this issue.
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
NSUInteger nSection =[myTableView numberOfSections]-1 ;
NSUInteger nRow = [myTableView numberOfRowsInSection:nSection];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:nRow inSection:nSection];
NSLog(#"No of sections in my table view %d",nSection);
NSLog(#"No of rows in my table view %d",nRow);
NSLog(#"Value of selected indexPath Row %d", [indexPath.row]);
NSLog(#"VAlue of Array arrOperationChk %d",[arrOperationChk count]);
}
This code appears to respond to something (the table?) being touched. You then ask the table how many rows it has in its last section and create an indexpath to that.
The table caches the number of rows in each section. If you have changed the number of rows, you need to tell the table, either by calling -insert/deleteRowsAtIndexPaths:withRowAnimation:, or by calling -reloadData. Otherwise the table has no way to know that it needs to re-query its datasource (which you provide).
Unless I'm reading this code wrong, aren't you just getting the index path to the last cell+1 of the last section? I wouldn't expect that to change.
If you want to get the selected cell, use the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
method in your UITableViewController object.
As other people have said, there's nothing in your code that would change the indexPath variable you've just created.
Also, the syntax [indexPath.row] looks wrong - you don't need the square brackets there unless you're calling a method. When you use the dot syntax like that on a pointer in Objective-C, you don't think of it as a method call (even though there is one, implicitly), but rather as a pseudo-instance variable as of a struct.
What is your big picture goal? If we understood what you are trying to achieve / what is the desired behavior, maybe a more useful answer will arise.