unnecessary uitableview delegate methods - uitableview

I inherited another developer code, but didn't understood the following code:
-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
return 0.0;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return nil;
}
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return nil;
}
Is anything can happen if i'll delete it ?
it seems unnecessary

If you don't need them just comment them out. You 'll be fine.

Those methods are just part of the UITablieView "appearance" of the table. It is the string that is displayed under a section in your TableView. You can just comment them out or delete them totally, this won't affect your program.

Related

Cannot see cells when I have Two UITableViews in one UIViewController

First of all this is not a duplicate as I have read through very similarly titled questions but they do not give me the correct answer!
So what I have done is:
First of all create a Table View with 13 unique (contain different buttons) prototype cells where the table views content is "Dynamic Prototypes". I then created a UITableViewCell class called blurCell.
I can see the new/second UITableView but there are no cells within it?
I currently have (for the first UITableView) the code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 25;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
}
But I did this because I was using a .xib for this table.
Do I need this kind of code for the new/second UITableView as I have done everything manually in the storyboard?
Please help?
In the ViewController.m (ImagesTableViewController) that contains these two tables I have the code:
#interface ImagesTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
Which is linked to both tables
Each tableView should be declared as its own property with the delegate/data source set to self most likely in viewDidLoad
You need to do something like this for the delegate/data source methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(tableView==firstTableView) return 25;
else if(tableView==secondTableView) return 2;
else return 15;
return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
if(tableView==firstTableView) return 1;
else if(tableView==secondTableView) return 2;
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{...}

Warning trying to call BOOL method from another method

I am following an iOS tutorial and my code is supposed to be correct, but I am getting an error which is not shown at the original tutorial code.
These are the concerning methods:
-(BOOL)tableview:(UITableView *)tableView canCollapseSection:(NSInteger)section {
if (section >0) return YES;
return NO;
}
And here the piece of code supposed to be correct, but throws an error:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return [self.searchResults count];
}
else {
if ([self tableView:tableView canCollapseSection:section])
{
if ([expandedSections containsIndex:section])
{
.../...
The error is shown at line
if ([self tableView:tableView canCollapseSection:section])
And this is the error message:
No visible #interface for 'ToDoItemsTableViewController' declares the selector 'tableView:canCollapseSection:'
Is there any visible error in the code?...
in your method you are calling tableView but it should be tableview. small 'V'
- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section
tableView : V should be capital, please check after -(BOOL):tableview part
Declare
-(BOOL)tableview:(UITableView *)tableView canCollapseSection:(NSInteger)section
in your .h file.
Also make sure that, you typed it correctly (any spelling mistake can also cause the issue).
You just need to declare your method
-(BOOL)tableview:(UITableView *)tableView canCollapseSection:(NSInteger)section
in yourViewController.h file.

UITableView - Section header. How to change text?

I have a project which uses Storyboards. I have a UITableView with Static cells and Group style.
I need to change the section text in one section depending on which selection is made in a segmented control (in another section)
I have found some solutions which indicate that you should use override this method:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
and trigger an update by calling:
[[self tableView]reloadSections:[NSIndexSet indexSetWithIndex:SectionToChange] withRowAnimation:UITableViewRowAnimationFade];
The problem is when I call reloadSections then all the rows and cells in the section in question get deleted. The text updates correctly thought but with this unwanted side effect.
I think I found the answer here: Changing UITableView section header without tableView:titleForHeaderInSection
It may not be very elegant but it seams to work.
I can trigger an update to only the section header with none of the unwanted side effects by calling:
[self.tableView headerViewForSection:1].textLabel.text = [self tableView:self.tableView titleForHeaderInSection:1];
So the only thing needed is to implement:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 1){
if (self.segmentX.selectedSegmentIndex == 0) // or some condition
return #"Header one";
else
return #"Header two";
}
return nil;
}
If you have implemented these functions then removing them should fix your problem:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{ if (section == 0)
{
lbl.text = #"abc";
}
if (section == 2)
{
lbl.text = #"2ndsectionbeigns";
}
return headerview;
}

Best approach: Displaying a "nothing here, yet" screen instead of empty tableView cells

When opening a UITableViewController, I'd like to display a screen saying "nothing here, yet" (c.f. screenshot of the Dropbox App below) instead of empty rows when there are no sections/rows. This is done in multiple Apps already, but I'd like to know if there is a recommended way of achieving this.
I can imagine multiple ways of doing this, including
adding a UIView as a subView to the view of the tableViewController
switching to a UIViewController and implementing the tableView behavior on my own
setting the UIView as the backgroundView and hiding the tableView cells
Thank you for all your ideas!
The way I usually do it, is displaying a single custom cell when there's nothing to display.
There is a new (as of June 2014) library called DZNEmptyDataSet handling exactly this case. It uses the first approach (UIView as a subview) described in the question. Pretty neat realisation.
You must be having some data array through which you can define few things, in table view.. You can achieve this in your tableview only with its delegate methods..
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if([dataArr count] == 0)
return 1;
return [dataArr count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if([dataArr count]==0){
return self.view.frame.size.height;
}
return 44.0;
}
and then
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if([dataArr count]==0){
//Create a cell with the no data image
tableView.scrollEnabled=NO;
}else{
//Create your default cell with data..
tableView.scrollEnabled=YES;
}
return nil;
}
I am just giving you an idea, its your choice of using it..

Delete when Table Rows are Swap

I have a table. When I swipe horizontally across a table cell a button appears asking if I want to delete the row.
I do not want that feature. However, I looked up the code up and down and I never see where does this feature is implemented.
Other rows on other tables do not behave the same way.
I think what you want to do is:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return NO;
}
in your table's viewController.
However, I looked up the code up and down and I never see where does this feature is implemented.
It's implemented by the table view. You can prevent cells from being edited at all by returning NO from your table data source's implementation of -tableView:canEditRowAtIndexPath:, and you can change what editing options are available for a given row by returning an appropriate value from -tableView:editingStyleForRowAtIndexPath:.
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// Detemine if it's in editing mode
if (self.editing) {
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
or
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}

Resources