custom searchResultsTableView - ios

I am looking to have custom UITableViewCells for my UISearchDisplayController. The searchResultsTableView is a readonly property. I have the delegate and datasource for the searchviewcontroller returning the custom cells, however, it seems to still be using the searchResultsTableView cells. What's the best way to have the search results using my own UITableViewCells?

When a search begins, a whole new UITableView is created. This new table view will use default settings and won't match your table view. In your UISearchDisplayDelegate, override searchDisplayControllerWillBeginSearch: in order to re-theme the new UITableView, like this:
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
// Re-style the search controller's table view
UITableView *tableView = controller.searchResultsTableView;
tableView.backgroundColor = [UIColor blueColor];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

I'm not sure I fully understand the question, but it sounds like you want two different UITableViewCell types (subclasses?) to be returned when you're typing a search, or when you're not searching. Is that right?
Like John said, and depending on how you hooked up the UISearchDisplayController, both the search results table view and your default table view will fire cellForRowAtIndexPath and willDisplayCellForRowAtIndexPath. In those methods you can check which tableView is asking for the cell and customize it as necessary. I customize the background, text and color of my cells and this approach works well for me. I can post code if you'd like.

Related

A UICollectionView bug?

I have a UICollectionViewController while doing the following:
Return a string array in collectionView:indexPathForIndexTitle:atIndex:
Set cell.contentView.backgroundColor = [UIColor redColor] on collectionView:cellForItemAtIndexPath:
(Notice that changing the background color of the cell is just for demonstrating the issue. The important thing is to show index titles on the right.)
The result (as following displayed) is that the content view takes the entire collection view width and it doesn't being affected (as it should) by the appearance of the index titles.
With UITableViewController I am doing similar steps:
Return a string array in sectionIndexTitlesForTableView:
Set cell.contentView.backgroundColor = [UIColor redColor] on tableView:cellForRowAtIndexPath:
Here, in contrast to the collection view behavior, the content view is "shrinking" as expected as following appear:
Can anyone tell if this is a bug or the expected behavior?
your question is not clear and obvious, but i will try to answer.
First: try to set UITableView's (actually UIScrollView's) property 'showVerticalScrollIndicator' to false (https://developer.apple.com/documentation/uikit/uiscrollview/1619405-showsverticalscrollindicator?language=objc)
Second: try to play with TableView 'contentInset', 'separator inset' properties. Perfectly, you should set 'separatorStyle' to 'none' and use your custom separator-cells, or conditionally set bottom line inside cells (https://developer.apple.com/documentation/uikit/uitableview/1614909-separatorstyle)
Third: check out how to customize Header of Table or Header/Footer of Collection:
https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614901-tableview
https://developer.apple.com/documentation/uikit/uicollectionviewdatasource/1618037-collectionview
One more important thing: you may always Examine a View Hierarchy. Check out this link https://developer.apple.com/library/archive/documentation/ToolsLanguages/Conceptual/Xcode_Overview/ExaminingtheViewHierarchy.html

Locate a specific content view in a UITableViewController using viewWithTag function

I need to locate a specific content view in a table view.
This table view is driven by a UITableViewController that is used to show my user with some parameters he can change in my app.
For one of those cell, I need to programatically add a UITextView that I had subclass (NSTextStorage) to change text attribute, have rich text editing and so.
So I need to find that specific cell (content view) to by able to call my .addSubview. I thought this you be as simple as adding a specific tag to this view and call the .viewWithTag later in my viewDidLoad. The problem is a got a nil!?!
Here's the little code snippet and a capture of my table hierarchy
override func viewDidLoad() {
super.viewDidLoad()
let specific = self.view.viewWithTag(99) //--> Return nil
let specific2 = self.tableView.viewWithTag(99) //--> Also nil
What I'm doing wrong or did I miss something? This's my first shot at this method (viewWithTag) and maybe there's something else I need to do?
Thx!
You can't call this on viewDidLoad, since you are using an UITableView, all those Cells will be only loaded AFTER viewDidLoad on the method tableView:cellForRowAtIndexPath:.
So if you want to insert anything on a Cell, you must use tableView:cellForRowAtIndexPath: to do that.
I'm not sure if viewWithTag works with TableView, but you shouldn't do that. To get a View for a specific Cell (after it being loaded on the method I said above) you can call the method below:
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[[NSIndexPath indexPathForRow:0 inSection:0]];
After a lot of search, I decided to go with a Container View.
So I place UIContainerView control inside my cell. This create a embed segue to a custom view.
In this view a can override the viewDidLoad and load my custom UITextView Control...
class CustomView: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// ... code of my super class UITextView with my custom NSTextStorage
self.view.addSubview(specialTextBox)
}
So now it works! :-)

When subclassing UITableView how to customize cell preparation

I want to customize a cell in my UITableView subclass. But I cannot figure out is there any way to do it without defining itself dataSource, because it's obviously override external dataSource.
So basically I want to be UITableView dataSource without rewriting this property.
I have already come up with some dirty workaround. I'm reloading the -setDataSource: method to keep UITableView dataSource itself and save incoming data source into an internal variable for passing the requests to it.
You only need to override cellForRowAtIndexPath: and make your cell modifications there. The datasource will populate the cell as usual.
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Calling super will let the datasource methods be called
UITableViewCell * cell = [super cellForRowAtIndexPath:indexPath];
//Do whatever to the cell here
return cell;
}
You can customize your cell in one of two ways: code or interface builder. The tableview datasource is the DATA you want to show, it has nothing to do with the presentation of that data.
What do you want to customize about your tableview? Changing fonts, font colors and background colors are easy to do. If you want to add additional ui elements, like images, more labels, then the quickest way is to use Interface Builder.

Stop UISearchDisplayController from showing empty cells

I've implemented a UISearchDisplayController on a fairly standard tableview (same datasource for table + search). The problem I am having is when the results don't fill the screen there are "pseudo" rows below the actual results.
I was able to set the background color, but can't find a good way to suppress these rows/separators. They seem decoupled from my numberOfRowsInSection: delegate response.
If I set the searchResultsTableView.separatorColor (green) it only changes the actual results rows.
I was able to change separatorStyle to UITableViewCellSeparatorNone, but then I have to manually recreate the separators on the actual results and there are edge cases (like the selection color covers up my view).
Is there a clean way to hide the rows pointed out in the attached screenshot?
You can probably implement this delegate method for the Search Display Controller:
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView;
{
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 1)];
footer.backgroundColor = [UIColor clearColor];
[tableView setTableFooterView:footer];
}
This will make those last few rows disappear. You can of course do this in any other method you choose. For example, you can do this in viewDidLoad of a UIViewController if you want the same effect on a normal UITableView.

iOS view similar to add/edit contact

I'm trying to create a view similar to add/edit contact in iOS and there are a few things that are happening, and I'm not sure how they are implemented. Any help in understanding is greatly appreciated.
For each section in contacts i.e. name, phone number, email, etc are these each their own tableview or are these sections within a larger tableview?
When clicking done when adding or editing a contact, the unused tableview cells disappear. Is this using deleteRowsAtIndexPaths:withRowAnimation: or is there a hide method I haven't found? If it is using that method, then when clicking the edit contact button, how does the view brings back these unused tableview cells?
When clicking on a cell in the tableview cell when editing a contact, you are able to change the text. Is this a textfield within a tableview cell or is it actually modifying the label of the tableview cell?
I am not looking for any specific code, as a fairly new programmer I am just trying to understand the strategies/best way to implement these features.
I tried a lot different ways to implement that. the easiest one: Subclass UITableViewCell and overwrite setFrame:. note that this is easy to achieve for grouped tables, but hard for plain ones. in the datasource's tableView:cellForRowAtIndexPath: create an object of this custom cell for the first section. use another identifier for cells of that section, so that only the correct cells will be re-used.
yes, I assume that. The controller has some sort of definition how many cells has to be show in edit mode and how many are actually used with some sort of information. you can easily create a array of indexPaths that must be deleted.
I would do it in tableView:didSelectRowAtIndexPath: by fetching the cell via tableView:cellForRowAtIndexPath:, hide the label and unhide or add a textfield and make this first responder.
code for 1.
the cell
#interface InsetCell : UITableViewCell
#property(nonatomic)CGFloat inset;
#end
#implementation InsetCell
- (void)setFrame:(CGRect)frame {
CGFloat inset;
if (_inset == 0) {
inset = 70; //default value
} else {
inset = _inset;
}
frame.origin.x += inset;
[super setFrame:frame];
}
-(void)setInset:(CGFloat)inset
{
_inset = inset;
[self setNeedsLayout];
}
#end
a project that uses similar code

Resources