Row number in UITableViewController does not conform the protocol function - ios

I use a dynamic tableViewController. I implement the numberOfSection to return 4, and the numberOfRowInSection return 1. These are the only changes I made for the blank TableViewController. But when I run it on simulator, it shows multiple cells, which I expected to be only four cells. Why is that?
Note: If I change the height of each cell to let the screen only fit four cells at a time, it will work fine since it won't let be me scroll down. So I guess is the tableViewController always try to fill the screen no matter how many cell it should display?

Yes, the UITableView will always continue to fill the screen with 'empty' cells of the default row height. To prevent this, you could add the following code in viewDidLoad:
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
(For Swift, check #VictorSigler's answer.)

In Swift:
self.tableView.tableFooterView = UIView(frame: CGRect.zeroRect)

Related

Removing unnecessary lines form tableview

I have two cells in my tableview, each with a text field. Following these two cells are many addition lines even though there are no cells to follow. How do I remove these lines to create a cleaner look.
Side Note:
The reason I didn't use static cells created through the storyboard is because the situation requires to create the cells through code.
And this is for Objective-C
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Swift 3
tableView.tableFooterView = UIView(frame: .zero)
To remove the extra shown empty cells just set tableFooterView to an empty view
tableView.tableFooterView = UIView()

Can not remove sub view from tableview custome cell

When table will be scroll it will repeat subview on cell, I am use below code for display embedded view on tableview custome cell. any one know so please help me. How to resolve this issues.
long tag = 100 + indexPath.row;
URLEmbeddedView *embeddedView = [custom.contentView viewWithTag:tag];
if ([custom.contentView viewWithTag:tag]) {
[embeddedView loadURL:#"" completion:nil];
[embeddedView removeFromSuperview];
}
embeddedView = [[URLEmbeddedView alloc] initWithFrame:CGRectMake(35, 60, 220, 110)];
//URLEmbeddedView *embeddedView = [[URLEmbeddedView alloc] initWithFrame:CGRectMake(custom.img_bgOther.frame.origin.x + 35, custom.img_bgOther.frame.origin.y + 50, 220, 110)];
embeddedView.tag = tag;
[custom.contentView addSubview:embeddedView];
[embeddedView loadURL:str_Website completion:nil];
Presumably the code in your question is taken from your cellForRowAtIndexPath: method. The problem you describe is a consequence of cells being reused. Cells for rows that have scrolled off screen are reused for rows that scrolling on screen. Suppose row 0 is scrolled off screen. It will have an embedded view with tag 100. And suppose it is reused for row 10. The above code will try to find a view with tag 110 and remove it from its superview. Since the tag does not match, the existing embedded view is not removed, and the new embedded view is added on top of it.
I would suggest you do things differently. You shouldn't be adding and removing the embedded view in cellForRowAtIndexPath. You have a custom cell, so just design the embedded view into it. Create a URLEmbeddedView property in your custom cell class, so you can access that subview without having to use viewWithTag. You can use that property to call the loadURL:completion: method with the correct values for the current row.
When cells are reused, the tableView will call the cell's prepareForReuse method. So override that method in your custom cell class, and clear the loadURL/completion at that point.

Section header view glitch in plain uitableview

I have a UITableView of type plain table view with two sections and a dynamic number of rows.
Also I am returning a UIView for the section header.
Everything works perfect except when the table reloads. The section header position moves to a random position until the view begins to scroll.
Autolayout within the header view and super view seem not violating.
I have set the table footer view either nil or show paginating animation.
And the table view is within a scroll view.
Any suggestion or solution why this glitch occurs for section header.
And this glitch only occurs in iOS 9
Problem for me was independently from refresh control, although it is part of my view controller.
It was related to estimatedRowHeight and rowHeight.
I found an empirical solution. My issue happened when going back and forth between view controller and the tableview reloaded.
Because my cells have variable height, I had:
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0f;
so I can deal with cell autoresize.
Apparently the problem was with the estimatedRowHeight. When I changed the value with the maximum cell row height the glitch disappeared. The table section wasn't jumping anymore. I used the view inspector to calculate the row height based on my worst use case.
Note #1
I don't know how it performs with a thousand cells. estimatedRowHeight should be used to calculate the entire content offset so considering the worst case might be
inefficient.
Note #2
When calculate the maximum cell height keep in mind to consider the case when the font size can be increased by the system preference, if the cell is registered to these changes.
My issues get resolved as follows.
Initially I have added refresh controller as a subview of table view.
_chatListRefreshControl = [[UIRefreshControl alloc] init];
_chatListRefreshControl.transform = CGAffineTransformMakeScale(0.7, 0.7);
[_chatListRefreshControl addTarget:self
action:#selector(refreshChatListView:)
forControlEvents:UIControlEventValueChanged];
[self.myChatsTableView addSubview:_chatListRefreshControl];
I have modified that to as follows: Changed the refresh indicator from table view to table view controller's property.
UITableViewController *chatTableViewcontroller = [[UITableViewController alloc] init];
chatTableViewcontroller.tableView = self.myChatsTableView;
_chatListRefreshControl = [[UIRefreshControl alloc] init];
_chatListRefreshControl.transform = CGAffineTransformMakeScale(REFRESH_INDICATOR_SCALE_FACTOR, REFRESH_INDICATOR_SCALE_FACTOR);
[_chatListRefreshControl addTarget:self action:#selector(refreshChatListView:) forControlEvents:UIControlEventValueChanged];
chatTableViewcontroller.refreshControl = _chatListRefreshControl;
[self addChildViewController:chatTableViewcontroller];

UITableView static cell's number of row is changed

I make one UITableView Controller had static cell. And I set number of rows 3 in Storyboard. But rows does not set 3, just be made more and more like this screen shot. I don't touch any programatic code. Did I have to make it programmatically?
That's the normal behavior of a UITableView. Even though you only have 3 rows, the view itself extends to the bottom, and it shows where the cells would be if you had data in them. To fix, do one of two things: customize the UITableView so the dividing line between cells is invisible [UIColor clearColor], or change the size of the UITableView's height depending on how many cells you have.
If you add a footerView to the UITableView then it will not extend all the way to the bottom.
I solve this problem on the story board.
Create one more cell. if you want 3cells, then make 4cells.
Make whatever you want on cell. put the UIButton or UILabel any way. But except 4th cell.
Expend your 4th cell's height, to the bottom.
And finally, check hidden in attributes inspector. It makes 4th cell hidden.
That's it!
And I add one image file. I hope it help your work. Thanks.
Simple solution is to set footer's frame to nil:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
/*........*/
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
return cell;
}
I would use a regular View Controller and insert a TableView of the required table height.
Then if you really want to you can do stuff with the cell height and label sizes.

Prevent UITableViewCells from creating as you scroll down

I'm creating a mail screen using which visually resembles the iOS native email app. It looks like this (Both images are of the same screen. First one is the top half and the second one is the rest of it).
The difference is my mail screen has more custom fields in addition to normal To, Cc, Subjet fields.
I'm using a UITableViewController to create this. Below is a code snippet which creates a cell (For each cell it's pretty much the same).
- (UITableViewCell *)tokenTableView:(TITokenTableViewController *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
self.tableView.frame = CGRectMake(0,0,320,320);
UIView *contentSubview = nil;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifierSubject];
if(!self.txtSubject) {
self.txtSubject = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
self.txtSubject.frame = CGRectMake(10, cell.frame.size.height / 2 - self.txtSubject.font.lineHeight / 2, tableView.tableView.bounds.size.width, 30);
self.txtSubject.placeholder = #"Subject";
[self setupMailData:indexPath.row];
}
contentSubview = self.txtSubject;
}
Say, I open up a draft. All the details in the input fields are filled and without changing anything, I hit send and it crashes the app. I know what's causing this. The problem is that normally the cells that are under the viewable portion of the screen gets created as you scroll down, right? But in this scenario, if I send it without scrolling down but those cells below the viewport don't exist thus it throws the error.
If I open the draft, scroll down and hit send, it works fine.
I need to know if there's a way to create all these cells at once. Even the cells that are below the viewport at first. Not depending on the user to scroll down.
I hope you have an idea about my situation. Can anyone suggest a solution?
Thank you.
follow steps:
Take uiscrollview and set scrollview frame as which you want to display.
Take uitableview as a subview of uiscrollview
set property Scrolling Enabled = NO (uncheck checkbox in .xib) of uitableview
call reloaddata method of uitableview
set tableview frame and contentsize of scrollview
tblEmail.frame = CGRectMake(yourXPos, yourYPos, yourWidth, tblEmail.contentSize.height);
scrollObj.contentSize = CGSizeMake(yourScrollWidth,tblEmail.contentSize.height+10);
so, the height of tableview is equal its contentsize. so, its create all cells at a time. and set contentsize of scrollview is equal tableview contentsize. so, the scrolling feature is worked like uitableview scrolling...
Use a Storyboard, add a UITableViewController and set the 'Content' to StaticCells.
Then you can define all the cells and their content in the Storyboard. You can even wire stuff up to IBOutlets in your UITableViewController subclass and they will all be there for you when viewDidLoad is fired ...
When using a Storyboard your code for getting the ViewController looks like:
[[UIStoryboard storyboardWithName:#"MyStoryboard" bundle:nil] instantiateInitialViewController];

Resources