How to always show tableView cell when i scroll - objective-c

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

Related

how cellforrowatindexpath works in objective c [duplicate]

This question already has answers here:
How does cellForRowAtIndexPath work?
(3 answers)
Closed 5 years ago.
I am new to objective-c programming language.I create a table and create all method of table View .But i don't understand about CellForRowAtIndexPath.Please tell me some one how it work.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
This is delegate method of UITableView. The returned object is of type UITableViewCell. These are the objects that you see in the table's rows. NSIndexPath has two this Section and Row.
It is called if you implement the UITableViewDataSource protocol in your view controller. A simpler way would be to add a UITableViewController class. I strongly recommend this because it Apple has some code written for you to easily implement the functions that can describe a table. Anyway, if you choose to implement this protocol yourself, you need to create a UITableViewCell object and return it for whatever row. Have a look at its class reference to understand re-usablity because the cells that are displayed in the table view are reused again and again(this is a very efficient design btw).
If your implementing custom cell then I will strongly recommend you use
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
to return only empty cell not set here. use thing like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyCart_Cell" forIndexPath:indexPath];
return cell;
}
and the use this delegate which will called just after cellForRow data source method
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell isKindOfClass:[CartTableViewCell class]])
{
CartTableViewCell *cell1 = (CartTableViewCell*)cell;
MyCartModel* data = [_myCartProductArray objectAtIndex:indexPath.row];
[cell1 setUpData:data];
}
}
and set data on UILabel in UITableviewcell custom class.

cellForRowAtIndexPath is called when there is no row

- (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.

Cell at UISearchController SearchTableView not responding to didSelectRowAtIndexPath

I have a main table view with a search display controller. I'm using a custom cell.h which implement
(void)setSelected:(BOOL)selected animated:(BOOL)animated
When I select the cell from main table, the code above changes the background color of this selected cell. When I choose to search from the search bar, the search display controller display the selected cell in the correct background color using
(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
However, this cell which is displayed at the search display controller does not responds to didSelectRowAtIndexPath and didDeselectRowAtIndexPath. Other cells, on the other hand, responds to both methods.
What I want to achieve is a synchronization between the main table and the search table view at search display controller where selected cells should appear selected in both main table and search table view and yet allows selection and un-selection.
Please advice. Many thanks.
I found the answer to my question:
Use
(void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView
And
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:[searchResult indexOfObject:currentFriend] inSection:0] animated:NO scrollPosition:0];
Instead of
(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
Hope it helps!

Dynamic tableview cells creation

I have to make a tableview with different cells in it. I have three preferences and the table depends on them. There may be 6 different tableviews - 1 cell, 1cell and 3cell, no cell, 2 cell and 3 cell and so on, this depends on preferences
That's the best way to do this?
Maybe someone knows good example, or tutorial on this
You could make just one UITableView with different sections.
Based on your section id you may be returning different cell in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
indexPath contains row & section values.
Also you might return different number of rows in a section with:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//pass value to numper of cells;
return 4 (or) 2;
}
//use this to add cell
[tableview 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];