Can I disable selection in specific UIPickerView item? - objective-c

I can find a post that teach to use delegate method to check if the row valid and then scroll to the right one if not.
But that's not I want. Can I disable the specific row after the pickerView initialized?
Like "Gray-out" the row and can't stop indicator on it.
Thank you, experts.

There are two parts to doing this.
Use -[<UIPickerViewDelegate> pickerView:viewForRow:forComponent:reusingView:] to return a custom view for each row, such as a UILabel. For the row you want to show up as disabled, you'll need to configure the view to reflect that disabledness.
In -[<UIPickerViewDelegate> pickerView:didSelectRow:inComponent:], you'll need to detect when the disabled is selected, and then use -[UIPickerView selectRow:inComponent:animated:] to "rollback" to a "valid" row.

You could also just leave a blank in the implementation, e.g.:
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if(row==0){
//Code
}
if(row==1){
//BLANK
}
if(row==2){
//Code
}

Related

How to dynamically create multiple UITableViews in one UIViewController

I'm new at developing with XCode and Objective-C and I hope you can help me.
The problem is, I have an UITableViewController with an UITableView (created with the InterfaceBuilder).
The cells under the section headers are expandable.
Now I want to dynamically create multiple UITableViews under the existing TableView.
The style will be the same like the existing TableView's style.
Could you tell me how it is possible to create these TableViews programmatically?
Thank you very much
Michael
From what you are saying try using a grouped table view. Check out this link for a quick overview, and go to the grouped table view section.
Edit found this example here:
Seems like it is what you are looking for. And a very cool idea also.
You'll have to just make your own custom header row and just put that as the first row of each section. Subclassing the UITableView or the headers that are on there now would probably be a huge pain and I'm not sure you can easily get actions out of them the way they work now. You could easily set up a cell to LOOK like a header, and setup the tableView:didSelectRowAtIndexPath to expand or collapse the section it is within manually.
If I were you I'd store an array of booleans corresponding the the "expended" value of each of your sections. You could then have the tableView:didSelectRowAtIndexPath on each of your custom header rows toggle this value and then reload that specific section.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
///it's the first row of any section so it would be your custom section header
///put in your code to toggle your boolean value here
mybooleans[indexPath.section] = !mybooleans[indexPath.section];
///reload this section
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}
}
You'd then setup your number numberOfRowsInSection to check the mybooleans value and return either 1 if the section isn't expanded, or 1+ the number of items in the section if it is expanded.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (mybooleans[section]) {
///we want the number of people plus the header cell
return [self numberOfPeopleInGroup:section] + 1;
} else {
///we just want the header cell
return 1;
}
}
You would also have to update your cellForRowAtIndexPath to return a custom header cell for the first row in any section.

Can I use a customized checkmark with UITableView's allowsMultipleSelectionDuringEditing set to YES?

A picture's worth a thousand words...
For a bit more background, I have a UITableView leveraging iOS 5's allowsMultipleSelectionDuringEditing set to YES. This results in the empty and filled edit controls being shown on the left of the cell any time the cell is in edit mode. This behavior is exactly what I want. I just want to change the appearance of these check marks.
I know it would be possible to write custom selection logic and basically roll my own version (like this and this), but that's what I want to avoid. The system is already in place, and I want to re-use as much of it as possible.
This is the closest I've come. It's simple and it works, while reusing almost all of the pre-baked system. It's also a giant hack however, and relies on exploiting the undocumented view hierarchy of UITableViewCell after a little runtime introspection.
In a nutshell, this simply hides the view normally responsible for showing the checkmark, allowing me to add my own view that can be shown in its place. I can then manipulate this stand-in view when the cell's selection or editing state changes...
To prevent the standard checkmark from appearing, all that's needed is a custom -layoutSubviews implementation. It's called, per the documentation, after both -willTransitionToState: and -setEditing:animated:, ensuring the state is always valid when either isSelected or isEditing changes.
- (void)layoutSubviews
{
[super layoutSubviews];
// Find the offending view, and quietly bury it...
for (UIView* subview in [self subviews])
{
// As determined by NSLogging every subview's class, and guessing which was the one I wanted
if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellEditControl"])
{
[subview setHidden:YES];
}
}
if ([self isEditing])
{
// Show the custom view however you want.
// The value of [self isSelected] will be useful...
}
else
{
// Hide the custom view.
}
}
I would still welcome a solution that's a bit more... kosher.

Prevents UISearchDisplayController from hiding the UiTable

Im new to objC and Im currently experimenting on UISearchDisplayController. Basically I have an array of string as my data and I use UISearchBarDisplayController to filter my data. Im able to retrieve the correct values when I enter my searchText into the search bar. However, the tableView disappears when my searhBar text is empty.
Would it be possible to prevent the tableView from hiding in this such case. What I want is to just to display all the values in my array in the table if the searchBar text is empty.
I checked the hidden/alpha/frame property of the table and tried to fix my issue here but I think Im in the wrong path here. I'm thinking if i need to subclass the UISearchDisplayController and override the [setActive:YES animated:YES];? Any hint would be appreciated.
You should recieve an event for any change to the search parameters, including when the string is empty.
If you change your implementation of that delegate method to check if the string is empty, you can return the original data instead of the filtered data. This should achieve what you're asking for without the need for subclassing.
I ended up working with UISearchBar and UITable.
So here are the scenarios I encountered.
Display the the UITable when the searchBar is clicked.
UITable shows all the data from my plist when searchText is empty.
UITable shows filtered results from my plist that matches the searchText.
Dismiss the keyboard on press of the search button but don't disable the cancel button on the searchBar.
Remove the UITable when cancel button is press.
I don't have the animation for showing the table for now but this works for me. Also, I allowed the user interaction and scrolling on my table during search so no overlay needed in this case. Glad its working now. :)
I created a sample project for those who might need. Here is the link. Apologies for a messy code and leaks issues. Just posted this to share. :)
You could try by always leaving a zero-space width characters in the search textfield :
static NSString* zsp = #"\u200B";
//In the UISearchBarDelegate
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if(searchBar.text.length == 1 && [text isEqualToString:#""])
{
searchBar.text = zsp;
return NO;
}
return YES;
}

Select text (content) instead of cell with NSCell

I'm currently working in a project with a NSOutlineView...
I use, of course, NSCell(s) and I need to let the ability to select text inside the cell...
Or at least... prevent the selection (and highlight) of the cells...
I search all options on IB, but can't found the right one...
Is there a way, programmatically or not, to prevent selection/highlighting of cell, nor let user select cell content ?
Thanks =)
That's not much NSCell related, maybe you're looking to implementing outlineView:shouldSelectItem: in your delegate.
On the NSCell, setEnabled:NO, may help too. From the documentation:
setEnabled:(BOOL)flag
The text of disabled cells is changed to gray. If a cell is disabled, it cannot be highlighted, does not support mouse tracking (and thus cannot participate in target/action functionality), and cannot be edited. However, you can still alter many attributes of a disabled cell programmatically. (The setState: method, for instance, still works.)
Try setting:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
You could also try overriding highlightSelectionInClipRect:, but I'm not totally sure this will work.
Let's take a quick example like the outline view below. There are 3 columns: firstName, lastName, and fullName.
In this particular example, let's say we want to only allow firstName and lastName to be editable while fullName (which is potentially derived from firstName and lastName) is not. You could set this up in Interface Builder by checking or unchecking the editable checkbox for the table column. To do that, click 3 times on one of the table columns (not the header, but inside the outline view); this first selects the NSScrollView, then the NSOutlineView, then an NSTableColumn:
You'd set the attributes like the following:
That provides a start by setting a default editable value for the entire column. If you need more control over whether a particular row's item value should be editable or not, you can use the outlineView:shouldEditTableColumn:item: delegate method:
#pragma mark -
#pragma mark <NSOutlineViewDelegate>
- (BOOL)outlineView:(NSOutlineView *)anOutlineView
shouldEditTableColumn:(NSTableColumn *)tableColumn
item:(id)item {
if ([[tableColumn identifier] isEqualToString:#"firstName"] ||
[[tableColumn identifier] isEqualToString:#"lastName"]) {
return YES;
} else if ([[tableColumn identifier] isEqualToString:#"fullName"]) {
return NO;
}
return YES;
}
If you want to control whether a particular row in the outline view is selectable (for example, you could prevent selection of a group item), you can use outlineView:shouldSelectItem:.
- (BOOL)outlineView:(NSOutlineView *)anOutlineView shouldSelectItem:(id)item {
// if self knows whether it should be selected
// call its fictional isItemSelectable:method:
if ([self isItemSelectable:item]) {
return YES;
}
/* if the item itself knows know whether it should be selectable
call the item's fictional isSelectable method. Here we
are assuming that all items are of a fictional
MDModelItem class and we cast `item` to (MDModelItem *)
to prevent compiler warning */
if ([(MDModelItem *)item isSelectable]) {
return YES;
}
return NO;
}

NSTableView selection & highlights

I have a NSTableView as a very central part of my Application and want it to integrate more with the rest of it. It has only one column (it's a list) and I draw all Cells (normal NSTextFieldCells) myself.
The first problem is the highlighting. I draw the highlight myself and want to get rid of the blue background. I now fill the whole cell with the original background color to hide the blue background, but this looks bad when dragging the cell around. I tried overriding highlight:withFrame:inView: and highlightColorWithFrame:inView: of NSCell but nothing happened. How can I disable automatic highlighting?
I also want all rows/cells to be deselected when I click somewhere outside my NSTableView. Since the background / highlight of the selected cell turns gray there must be an event for this, but I can't find it. I let my cells expand on a double click and may need to undo this. So getting rid of the gray highlight is not enough.
EDIT: I add a subview to the NSTableView when a cell gets double clicked and then resignFirstResponder of the NSTableView gets called. I tried this:
- (BOOL)resignFirstResponder
{
if (![[self subviews] containsObject:[[self window] firstResponder]])
{
[self deselectAll:self];
...
}
return YES;
}
Besides that it's not working I would need to implement this method for all objects in the view hierarchy. Is there an other solution to find out when the first responder leaves a certain view hierarchy?
I wanted to achieve a similar solution (with an NSOutlineView but this has no difference): when clicking inside the outline view BUT not in a row with a cell (for instance at the empty bottom of a source list), I wanted the currently selected row to be deselected. I ended up with this little piece of code that might be of some help.
In a NSOutlineView subclass, I've put:
#implementation ClickOutlineView
- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint pointInWindow = [theEvent locationInWindow];
NSPoint pointInOutlineView = [self convertPoint:pointInWindow toView:nil];
int rowIndex = [self rowAtPoint:pointInOutlineView];
if ([theEvent clickCount] == 1 && rowIndex == -1) {
[self deselectAll:nil];
}
else {
[super mouseDown:theEvent];
}
}
#end
No doubt it's too late to help the question's poster, but this may help others who come across this on Google (as I did).
You can prevent row selection by selecting 'None' in the 'Highlight' drop-down menu, in the attributes inspector in Xcode 4.
There is also an answer on SO here for setting this programmatically.
Overloading -highlight:withFrame:inView: should be correct. How did you do the overload? Does an NSLog() statement indicate that your code is running? You should also look at NSTableView's -highlightSelectionInClipRect:, which may be more convenient for this.
In order to do something (such as unselect the current selection) when the user clicks outside the table view, overload -resignFirstResponder on NSTableView.