UISearchbar Static Tableview - ios

Is it possible to have a UISearchBar on Static Cells, I really need the static cells, and there are like 40 of them, so how can I use the UISearchBar in it?
My Cells are filled with a button (Every name has another button (Every Cell has another name)).
So basically I need to search for the button text onto of each cell, and for that show the cells that contain the search string.
Can someone provide help please?
Thanks for your time and effort.

When you use static cells, as you probably already know, you don't implement the delegate methods in your UITableViewController. However, if you want to only display some of your static cells (or change the ones displayed), you can implement the delegate methods and call super on them after making whatever changes you need.
So by this I mean... If I have a static table view with 1 section, 3 cells, and I only want to display the first cell and the last cell, I would implement - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section, and have it return 2. Then I would implement - (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath, and when it gets called with the index path with section 0, row 0, I'll call the superclass method with section 0 row 0. And when it gets called with section 0 row 1, I'll call the superclass with section 0 row 2. Basically, I'm mapping from indices of rows that will be displayed to the indices in the complete static table as defined in the storyboard.
However, I would suggest that you don't need to use static cells at all, and it will be a lot simpler. You won't have to deal with this mapping to make this work. You would maybe want to use static cells if your cells have UITextFields in them, or some other type of view that could cause problems for reuse. But in your case, a dynamic table would be much better to use because you don't have any content in a cell that would need to possibly be saved if a cell were going to be reused at a different index.
If you go the dynamic table route, you just need to implement the delegate for the search bar, and as you type you will store the list of filtered results in an array stored in an instance variable or property, and reload your table, using that array as the data source.

Related

Setting Content on 3 Separate Static TableViewCells that are of the same type

I have a tableview where a single prototype cell is used only 3 times. This cell has some buttons on it that trigger displaying and hiding content within the cell.
I could reuse the cells, but that requires a lot of resetting each cell so that if I expand A's content, B's content isn't expanded when it loads. Furthermore, this requires me to keep a state record in the Controller class, where I would prefer to have this all handled by the Cell itself for modularity. In other words, the amount of work to keep each cell in the correct stage seems inefficient.
What would be the best way to go about this? Do I use static cells? Is there a way to instance 3 separate cells of the same type and place them in the TableView?
You certainly can have the modular part of the code you need inside the cell itself. If you don't want to reuse the cells, then add a custom initializer with a case for each cell type. Inside your delegate method cellForRow:
return CustomCell(type: .myCustomType)
If you do it this way, you can add a switch inside the cell's initializer & set up the cells according to their types that way. And i'm assuming by "static" you mean just three instances. You "add" cells to the table by telling the delegate the number of cells that it will need, which will in turn call cellForRow x times.

Pattern for no data in a section in collectionView/TableView

I am contemplating on what is the recommended way to display a notice that there is no data for a specific section in a collectionView/TableView
One way is to create a special cell and to put that in instead of the data cells. That feels odd, since the "empty notice" cell doesn't correlate with the data, which means I would need to spread a lot of conditions in didSelectItem, configuring the cell, etc
Using https://github.com/dzenbot/DZNEmptyDataSet is appropriate only when the entire view is empty, not in a specific section
Another way would be (which is what I do now) to insert a UIView into the place where the data would be as a subview of the collectionview, But this also would require maintenance when reloading the data, scrolling, tapping. Also this requires calculation of where to place the view, which means I need to change it per collectionview, since it is not part of the collectionviewlayout
Is there a recommended pattern to deal with these situations?
Maybe you shouldn't have that section at all if the section contains empty data. Reduce section count by 1 at - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView datasource method and recheck the cell to be displayed during - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath datasource method.
In reply to you, I think you would have some type of "Add" button to add comments, photos, friends, etc. Make the option to add a comment or photo less obtrusive than taking an entire tableView row, and omit the comment or photo section, if there are no comments or photos.
If you put an "Add comment" or "Add photo" row while displaying details, users will likely get tired of unnecessarily reading that, as they use the app.
Note that tableViews generally handle insertion in editing mode, and when not editing, no section is shown if it is empty.

How to add static tableviewcells on runtime in IOS

I'm new in objective C.
I am using an static table view controller to load data related to my Class GAME.
I have 2 sections, in one section are cells related to the game, and in the last section y want to add the players who were in the game, which can be as many as I want. For this I created a button in the last cell of the first section addPlayer.
I want to create a new row in the 2nd section each time I touch addPlayer. how can I achieve this?
Thanks!
PD: Do I need to have the first row of the section 2 added by story board? Don't I?
if you want to add cells each time you add a player you could have, for example, a NSmutableArray which will contain the players and when you create a player you add it to that array and call the table's [yourTable realoadData] method. Then, your UITableViewController cellForRowAtIndexpath: method should loop through that array and add the cells dynamically. Use the array's count to keep track of the indexPath. You might have to keep track of the section so that you add the initial cells of the first section (the Date cell, the Pozo cell and the button cell) Keep in mind you will need to add a prototype cell in the Storyboard and set its identifier so that you may reuse the cell otherwise you will have to keep creating and adding cells to the table, which is not efficient.
Let me know if it works or if you have more questions!
You can't add static rows at runtime, you should go for dynamics.
What you can do, but only if you are using a Table View Controller, is create the maximum number of rows that you need, and override the – tableView:heightForRowAtIndexPath: to return zero height rows, at the indexPath that you don't need to show.

iOS UITableView with UIPickerView

I need to create a UITableView with multiple columns. In that, there will be a constant block on the top row of the table. When the user scrolls through the table, the row which is there in that block, will be picked. Basically it is a table view with UIPickerView.
I wish I was able to upload an image to explain this.
I'm not sure how to go about this. This is a hybrid of UITableView and picker view. Can I implement this is as a picker view of course with multi column (the block acting as a custom picker)?
Please help.
Thanks,
~Vishal
YOu can get an index to the data represented in your top row using the UITableView method:
- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point
where point would be (CGPoint){0,0} (or some point which intersects with your block/row 1)
or
- (NSIndexPath *)indexPathForRowsInRect:(CGRect)rect
where rect would relate to you "block".
you will want to manage the scrolling so that the table comes to rest with the top of a row aligned with the block or top of the table. This UITableView method may prove helpful, you should trigger it when your tableview comes to rest after a scroll:
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath
atScrollPosition:(UITableViewScrollPosition)scrollPosition
animated:(BOOL)animated
with UITableViewScrollPositionTop as the scroll position
I expect you want each column to scroll independently. In that case you would need one tableView per column. Then you just need to make sure you know which tableView is doing the message sending in your respective delegate and datasource methods (easy to manage as a pointer to the tableView is the first parameter sent in those methods).

Alternative method for getting reference to a UITableviewCell?

The design of the app I am working on, specifically the tableview part is quite complicated.
There are around 5-6 methods in which I need to get a reference to particular cell from the table view.
What I do not like is that I have to use the
-tableview:cellForRowAtIndexPath
This is a datasource method and does a lot of heavy lifting. Custom cell configuration, dequeueing, getting data for particular cell..etc.. The point is all this code is executed again,even if it does not make any sense at all. The cell object is completely loaded already.
Am I right in my observation and if yes, is there a working tested and lightweight solution?
Perhaps there could be an index of references to all cells. Asking it, I would immediately get the reference without the datasource code.
UITableView has cellForRowAtIndexPath: method.
From UITableView reference:
Returns the table cell at the specified index path.
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
Parameters
indexPath The index path locating the row in the receiver.
Return Value An object representing a cell of the table or nil if the cell is not visible or indexPath is out of range.

Resources