Using the same table view cell in multiple view controllers - ios

I have a custom table view cell class, which I use in several view controller's table views. I am adding a feature that gives the user the ability to delete a table view cell and I'd like to remove the cell from the table view when they do so.
Using a delegate is one way to achieve this, so that when the user deletes the table view cell, the table view cell class informs the view controller, through the delegate, that it was deleted and to remove it from the table view. However, this would mean I'd have to do this for each of my several view controllers, which I don't want to do.
So my question is, how can I implement this in such a way that I only have to write the code to remove the table view cell from the table view once?

Subclass the UITableView. Create a protocol like:
protocol customTableViewDelegate {
func deleteTableViewCell()
}
Then give the subclassed UITableView a variable of this type:
var removeCellDelegate: customTableViewDelegate?
And then instead of adding a normal UITableView to your view controller, add the custom one and set the removeCellDelegate to the view controller. Then implement the function to do whatever you want.

Related

implementing a search Bar

I am working on "implementing a search Bar"
When I work on "group table with multiple sections", Main.storyboard have View Controller and Table View it is run and display well like address list.
However, when I try to add the search Bar, I don't know how to deal with Main.storyboard. If I only have Table View Controller
I run it and display nothing.
If I have View Controller && Table View ,and also have Table View Controller, It have an error.
The code from the book is right, but to create Main.storyboard, the book explain very implicit.
By the way, what different between Table View Controller and use View Controller && Table View
The TableViewController comes with a tableView whose delegate and datasource are already set to be that TableViewController. This is normally used when it's ok for the table to take up the entire screen. The tableView is a normal view that you can add to any relevant ViewController. If this is the case, you must set its delegate and datasource and ensure that the corresponding ViewController conforms to UITableViewDelegate and UITableViewDataSource. The search bar would be a textField that would need a its delegate set. You can then write a search function to find whatever your text the user has in the textfield.

move tableview delegates to different class

I have one view controller containing lots of operations. Most of the views are created dynamically. To separate out code I created a class as a myClass of NSObject where I created a function showMyTableView. This function adds a tableview as subview. but I have to write all tableview delegates inside view controller. How I can move require delegates to myclass?
Tomorrow I may require to add subview containing buttons for which I should write action in separate class.
How I can accomplish same?
your new class should handle data only, not ui.
for different table cellview consider use separate tableviewcell subclasses and provide them data models. let them configure themselves
(By 'table view delegates', I assume you mean your table view's delegate and dataSource?)
You can certainly move this stuff to another class. You would simply set the delegate/dataSource of your table view to an instance of MyClass, in your view controller, like so:
self.tableView.delegate = instanceOfMyClass;
MyClass probably shouldn't be responsible for creating the table view and adding it to your view controller's view though. Your view controller should be responsible for this itself.

Update Table View on command from View Controller

New to iOS programing and objective-c and am getting stuck on something which I am guessing is simple. Here's my question:
I have a simple, single view application with two text fields, a button and a table view. The idea is that I want to take the text from the two text fields and populate the next available cell in the tableview with this text. I've got everything sorted out except for how to update the contents of the table view on command. Ideally, I'd like the table view to be updated upon pressing the button.
I have a View Controller which is serving as the data source and delegate for the table view.
I am aware of the [tableView reloadData] method, but it requires the appropriate tableView variable within scope. This is fine in say a delegate method like tableView:didSelectRowAtIndexPath, and I've gotten it update upon selection of any cell, but what if I want to update the table view when an action unrelated to the table view is performed, say when the button is pressed? There is no tableView variable in scope of this method to call reloadData from (or is there)?
So I guess my question is: is there a way to access the table view variable from the rest of the view controller for the purpose of calling reloadData OR is there a method I'm not aware of that would facilitate this?
Thanks for any help!
Why do you you create an outlet for your table view in view controller?
Your view controller will be delegate and data source for the table view (you should set in interface builder or from code:
self.tableView.delegate = self;
self.tableView.dataSoure = self;
I am assuming that self.tableView - is the outlet that I mentioned. When ou have an outlet you can easily update table view after pressing the button: e.g you add new value to an array that is used for filling table view and call
[self.tableView reloadData];
If you do not want to reload all table you can use method insertRowsAtIndexPaths:withRowAnimation:
Actually you have to read next manuals as well:
https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/AboutTableViewsiPhone/AboutTableViewsiPhone.html
This references are really useful.

Adding elements to top and bottom of UITable View Controller

I am new to objective C. I have a small app where I have used a UITableviewcontroller(TVC). I need to add four elements.,label and button above TVC and a label and button below TVC. But it is allowing only to add 2 elements., one above TVC and other below TVC. Also added element is occupying the entire width of screen. Can I overcome this restriction???
Customize the table view cell – instead of using the default style of table view cell.
this link is helpful you with sample project
http://www.appcoda.com/customize-table-view-cells-for-uitableview/
These two are tableview properties.
tableHeaderView
tableFooterView
By using the above properties you can show your own custom view on top / bottom of table view.
sample code
[self.tableView setTableHeaderView:yourHeaderView];
[self.tableView setTableFooterView:yourFooterView];
Write this code in viewDidLoad.
UITableViewController is kind of a screwed up class, in my opinion. A UITableViewController can only manage a table view, and nothing else. If you want a more complex layout where you have a table view on part of the screen, and then other view objects elsewhere, you can't do that with a UITableViewController.
However, with iOS 6 and later, there is a pretty easy solution.
You can embed a UITableViewController as a child view controller of another view controller.
What you do is to create a parent view controller with everything you need. Then you create a container view on your parent view controller and size it to contain your table view. Then you create a separate scene in your storyboard that defines a table view controller. Finally, you control-drag from your container view to your UITableViewController, and create an embed segue.
I have a project on github that demonstrates this technique:
Demo project using UITableViewControllers as child view controllers

only one section header

I'd like to have the section header (for an UITableView) for the uppermost cell only , a sort of header for the table that sticks to the top, showing some additional information about the uppermost cell.
is it somehow possible? if not (as I suppose since I've carefully read all the documentation) do you have any idea how to replicate this behaviour?
You need to create your own view to use as the header. It will be simplest to make your custom header view be a sibling of the table view, and position it so that it's above the table view on the screen.
Since UITableView is a subclass of UIScrollView, your table view's delegate is also a scroll view delegate and receives the UIScrollViewDelegate messages. You want to implement this method:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
Each time your delegate receives that message, you want it to look at which table view row is at the top of the table view (using [tableView indexPathForRowAtPoint:tableView.contentOffset]) and update the contents of your custom header view accordingly.
Read the Apple documentation for UITableView. The property you're looking for is tableHeaderView.
This is hard (not impossible) with UITableViewController because it forces the tableView to be the root view.
If you implement your own controller, inherit from UIViewController instead of UITableViewController.
You must adopt the data source and delegate protocols, and implement the methods appropriately, but then you have a normal view as your root view. You can then add a UITableView in any location and size you want, with anything you want around it.
The only real restriction is static table views you build in IB. However, in that case, you can implement view controller containment, and just parent your table view controller into another controller, and give it a specific view to take over.
The first option is dead simple, but the second is an advanced technique, and you need to understand view controller containment to do it right.

Resources