cellForRowAtIndexPath is called when there is no row - objective-c

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([BGSearchParameter defaultSearchParameters].fetchedResults.count==0)
{
PO(#([self.tableView numberOfRowsInSection:0]));
PO(#([self tableView:nil numberOfRowsInSection:0]));
PO(#(indexPath.row));
while (false);
}
This is the result
self.tableView numberOfRowsInSection:0]): 20
#([self tableView:nil numberOfRowsInSection:0]): 0
Now, I do not know who call that cellForRowAtIndexPath.
Here is the screenshot
The debuggin windows shows that the main thread somehow call cellForRowAtIndexPath.
I usually fix this issue by returning some random UITableViewCell that's never displayed. Howerver, I think it's a bug.
What functions can call cellForRowAtIndexPath anyway besides tableReload and why it doesn't call
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
first?
It seems that my tableView still think there are 20 rows even though there are only 0 rows.

The issue seems to be your tableview is not refreshing. Please include this line in your code so that it will refersh each time:-
[yourTableView reloadData];

You can use the parameter "tableView" of this method to check whether the right instance of "UITableView" is called. For example, in the delegate method" - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section" add these code:
if(tableView== youwanttocheckTableView) {
//do what you want to do
}
You can also check which TableView instance have set its delegate equals "self" in this class.

Related

How to always show tableView cell when i scroll

I need to always show cell with segmentControll, how can i do it?
There are a couple of ways to do this. Here is what I would do.
1
Don't put the segmented control as part of the table. Simply move it outside the UITableView. With that being said, if you are using a TableView Controller, you have to switch to a ViewController and add TableView to it.
2
This is a way to create a header for your table. Maybe that's what you want.
Use this method if you want a title:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
Or this one if you want a custom view:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
And for the height:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

Getting the header view height of a section

I've implemented the
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
delegate method and within that I implement this piece of code
UITableViewHeaderFooterView *header = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:#"paymentFormHeader"];
header = (UITableViewHeaderFooterView *)view;
Then I try the and assign a UIView from the header with
UIView *header = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:#"paymentFormHeader"];
and get a null value in return. This is my first time using this method so I'm probably not understanding it correctly and I noticed that it didn't ask for an indexPath. Any help with this would be greatly appreciated. Thanks.
There are a few issues here. The first is that you're not using the right method to set up the header. The method you're using (..willDisplayHeaderView..) is there to let you know when a header you've already set up is about to be displayed so you can do any additional setup or tracking after that point.
You will want to implement 2 methods to get this working:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
and
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Your next issue is how you are trying to dequeue the view. You either need to check for a nonexistent dequeued view and initialize one manually with that reuse identifier, or just register the appropriate class in advance. I recommend registering it in advance. So to fix this, in viewDidLoad or at some point after your UITableView has been initialized, register the class like this:
[myTableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:#"paymentFormHeader"];
That will make sure you will always get a valid initialized view of that type for that reuse identifier when you try to dequeue it.

Min and Max button functionality in UITableVIew

I want to do min/max (expand/collapse) button functionality.Please see bellow image for more information.Is anybody know how to develop this functionality.I have some examples but those are not like exactly what i want.Please help me to solve this problem.
I've got a similar kinda requirement few days back. I have done this using custom tableview cell.
Firstly, you have to pass the indexpath of each row to the cell. For this i have taken a property in custom tableview cell.
i.e, #property(nonatomic,assign) NSIndexPath *indexPathOfCell;
Now, you have to pass the indexpath of row in datasource method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
And you have to write a delegate method in custom tableviewcell so that when the user clicks on the min/max button you can reload that cell through this delegate call.
-(void)reloadCellWithIndexPath:(NSIndexPath*)indexPath;
You have to set the view controller as delegate for the custom tableview cell in which you are populating the table. In that class you have to maintain an NSMutableArray.
NSMutableArray *selectedIndexPaths;
When ever the reloadCellWithIndexPath gets called you can simply do same.
-(void)reloadCellWithIndexPath:(NSIndexPath*)indexPath{
if(![selectedIndexPaths containsObject:indexPath]){
[selectedIndexPaths addObject:indexPath];
}else
[selectedIndexPaths removeObject:indexPath];
[tableView reloadData];
}
In heightForRowAtIndexPath method you can change the height for a particular row.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
BOOL cellModeChanged = NO;
for (NSIndexPath *indexpath in selectedIndexPaths) {
if(indexpath.section == indexPath.section && indexpath.row == indexPath.row)
cellModeChanged = YES;
}
if(cellModeChanged)
return 400.0;
else
return 100.0;
}

tableView:cellForRowAtIndexPath: never invoked but rows and headers are > 0

A tableView in my UIViewController has as data source a NSMutableArray.
After I've added a new object to the mutable array, I invoke [tableView reloadData], which correctly invokes:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
returning the correct update amount of elements in the array. However the method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
is not called afterwards. I thought when I reloadData of a table view such method was invoked.
The sections method:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView
always returns 1, in my code.
Thanks
What does the
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
method returns?
Have you managed it to return the count of your data source which is a NSMutableArray as you have mentioned?
In this method call :
[array count];
It was a thread issue... Ive fixed it with:
dispatch_async( dispatch_get_main_queue(), ^{
[table reloadData];
});

Cannot make table re-order to work

From the UITableViewCell showsReorderControl docs:
For the reordering control to appear, you must not only set this property but implement the UITableViewDataSource method tableView:moveRowAtIndexPath:toIndexPath:. In addition, if the data source implements tableView:canMoveRowAtIndexPath: to return NO, the reordering control does not appear in that designated row.
I've got the both in my tableviewController :
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
NSLog(#"move from:%d to:%d", fromIndexPath.row, toIndexPath.row);
//just for test
}
My cell properties including re-order controls :
And yet I can't see the re-order control, what am I missing?
Have you put your UITableView into editing mode via
[tableView setEditing:YES animated:YES];
?
The first method you posted is needless, by the way, as that is the default behavior.
I think you need to actually get into edit mode for the table. Either try it in code when your view appears or create a button that will do it.
[tableView setEditing:YES animated:YES];