Clickable url link in NSTextFieldCell inside NSTableView? - cocoa

I have a NSAttributedString that I'm using in a NSTextFieldCell. It makes several clickable url links and puts a big NSAttributedString inside the NSTextFieldCell. Whenever I am viewing the NSTextFieldCell normally and it's highlighted, I cannot click on the links.
If I set the TableView so I can edit each column or row, when I click twice, go into Edit mode and view the NSTextFieldCell contents, my links show up and are clickable. When I click away from the row, I can no longer see clickable links.
I have to be in "edit" mode to see the links or click on them.
I feel like there's some setting I'm just missing.

I don't think the tech note answers the question, which was how to put a link in an NSTableView cell. The best way I've found to do this is to use a button cell for the table cell. This assumes that only links will be in a particular column of the table.
In Interface Builder, drag an NSButton cell onto the table column where you want the links.
In your table view delegate, implement tableView:dataCellForTableColumn:row: as follows:
- (NSCell *) tableView: (NSTableView *) tableView
dataCellForTableColumn: (NSTableColumn *) column
row: (NSInteger) row
{
NSButtonCell *buttonCell = nil;
NSAttributedString *title = nil;
NSString *link = nil;
NSDictionary *attributes = nil;
// Cell for entire row -- we don't do headers
if (column == nil)
return(nil);
// Columns other than link do the normal thing
if (![self isLinkColumn:column]) // Implement this as appropriate for your table
return([column dataCellForRow:row]);
// If no link, no button, just a blank text field
if ((link = [self linkForRow:row]) != nil) // Implement this as appropriate for your table
return([[[NSTextFieldCell alloc] initTextCell:#""] autorelease]);
// It's a link. Create the title
attributes = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSFont systemFontOfSize:[NSFont systemFontSize]], NSFontAttributeName,
[NSNumber numberWithInt:NSUnderlineStyleSingle], NSUnderlineStyleAttributeName,
[NSColor blueColor], NSForegroundColorAttributeName,
[NSURL URLWithString:link], NSLinkAttributeName, nil];
title = [[NSAttributedString alloc] initWithString:link attributes:attributes];
[attributes release];
// Create a button cell
buttonCell = [[[NSButtonCell alloc] init] autorelease];
[buttonCell setBezelStyle:NSRoundedBezelStyle];
[buttonCell setButtonType:NSMomentaryPushInButton];
[buttonCell setBordered:NO]; // Don't want a bordered button
[buttonCell setAttributedTitle:title];
[title release];
return(buttonCell);
}
Set the target/action for the table to your delegate and check for clicks on the link column:
- (void) clickTable: (NSTableView *) sender
{
NSTableColumn *column = [[sender tableColumns] objectAtIndex:[sender clickedColumn]];
NSInteger row = [sender clickedRow];
NSString *link = nil;
if ([self isLinkColumn:column] && (link = [self linkForRow:row]) != nil)
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:link]];
}
Now the link looks like a link, but a click on it is actually a button press, which you detect in the action method and dispatch using NSWorkspace.

Have you seen this technical note from Apple regarding hyperlinks?
Embedding Hyperlinks in NSTextField and NSTextView

Related

cocoa: how to create single column table view with checkboxes and labels

I have a fixed number of text items I want to display with checkboxes to select/deselect in a cocoa tableview. Exactly like a listbox of items with checkboxes in Microsoft MFC.
If I drop the section labels and checkboxes into the TableView they don't seem to belong to the tableview. Note: the labels are to divide/designate the sections for the checkbox items.
What are the correct steps?
Thanks.
You can make a CustomCell class: take a NSTextField and NSButton(for checkbox)-
set UIButton state in IB:
for default state - set non selected image.
for selected state - set selected image.
In CustomCell.m file
-(void)setSelected:(BOOL)selected
{
[checkBoxBTN setSelected:selected];
}
you can do this in also cellForRowAtIndex method in your tableView class.
your table data source should be a ModelClass or a dictionary with two keys like title and its state(selected/nonSelected).
In didSelectAtIndexPath method you need to update your dictionary like:
NSMutableDictionary * dic = [yourDataSourceArray objectAtIndex:indexPath.row];
NSNumber *state = [dic valueForKey:#"state"];
[dic setValue:![state boolValue]];
You need to modify your cellForRowAtIndexPath Method to show prev selected/nonSelected state
NSMutableDictionary * dic = [yourDataSourceArray objectAtIndex:indexPath.row];
NSNumber *state = [dic valueForKey:#"state"];
cell.title = [dic valueForKey:#"title"];
[cell.checkBoxBtn setSelected:[state boolValue]];
Edit
To create Label:
NSTextField *textField = [[NSTextField alloc] initWithFrame:NSMakeRect(10, 10, 200, 17)];
[textField setStringValue:#"My Label"];
[textField setBezeled:NO];
[textField setDrawsBackground:NO];
[textField setEditable:NO];
[textField setSelectable:NO];
[cell addSubview:textField];

UIImage doesn't show unless i click on my table cell

I've spent some time writing an xcode application today. I'm using a UITableView with a seperate xib for the table view cell. All is working well besides one quirky thing. In my code I set my image on the table cell xib from an array. When i run the app I've found out that the image does not appear on the table cell until I click on the table cell. And when I click on a different cell, the image on my previously selected cell disappears.
Funny thing is that I set my label on the table cell xib exactly the same way however I don't get the issue with the label.
Here is my code. I would greatly appreciate some help.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = nil;
cell = [tbHealthCheck dequeueReusableCellWithIdentifier:#"CONTENT"]; //all the cells in this table are named content (identifier for all cells in table)
if (cell == nil){
[[NSBundle mainBundle] loadNibNamed:#"FirstViewTableCell" owner:self options:nil];
cell = tcHealthCheck;
tcHealthCheck = nil;
}
UILabel *l1 = (UILabel *) [cell viewWithTag:0];
[l1 setText:[healthCheckCategory objectAtIndex:indexPath.row]];
l1.font = [UIFont fontWithName:#"arial" size:14];
[l1 setTextColor:[UIColor blackColor]];
UIImageView *img1 = (UIImageView *) [cell viewWithTag:1];
NSString *status = [healthCheckStatus objectAtIndex:indexPath.row];
if ([status isEqualToString:#"RED"])
{
img1.image = [UIImage imageNamed:#"red-light-badge.png"];
}
else if ([status isEqualToString:#"YELLOW"])
{
img1.image = [UIImage imageNamed:#"yellow-light-badge.png"];
}
else if ([status isEqualToString:#"GREEN"])
{
img1.image = [UIImage imageNamed:#"green-light-badge.png"];
}
return cell;
}

How To Provide Search-As-You-Type Filtering With UICollectionView?

I have a UICollectionView added to a ViewController. The UICollectionView displays a grid of items with peoples names on each cell.
I would like to add search as you type functionality that will filter the UICollectionView as the user types in their name to a searchbar or UITextField on the ViewController so that it doesn't scroll with the content in the UICollectionView.
Even though a few places I have read that UICollectionView is similar to UITableView I don't see that implementing this filter/search functionality is the same in UICollectionView as it is with UITableView and need some help with it.
Does anyone have a good example of doing this search/filter as you type functionality with UICollectionView?
I solved it by doing the following (hope it helps somebody else):
I populate my UICollectionView from CoreData into an NSArray:
self.allCVData = [context executeFetchRequest:fetchRequest error:&error];
then add my NSArray to an NSMutableArray so that I can use that for filtering on the UICollection:
self.filteredCVData = [[NSMutableArray alloc] initWithArray:allCVData];
Added a UITextField and called it searchField.
Added a selector in viewDidLoad:
[mytextfield addTarget:self action:#selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
Added a method to accept changes in the UITextField:
-(void) textDidChange:(id)sender
{
UITextField* searchField = (UITextField *) sender;
if(searchField.text.length == 0)
{
self.isFiltered = FALSE;
[self.filteredCVData removeAllObjects];
[self.filteredCVData addObjectsFromArray:self.allCVData];
}
else
{
self.isFiltered = true;
[self.filteredCVData removeAllObjects];
self.filteredCVData = [[NSMutableArray alloc] init];
LogInfo(#"Before Looping in allCVData Array.");
for (OLPerson* person in allCVData)
{
NSRange firstnameRange = [person.firstname rangeOfString:searchField.text options:NSCaseInsensitiveSearch];
NSRange surnameRange = [person.surname rangeOfString:searchField.text options:NSCaseInsensitiveSearch];
if(firstnameRange.location != NSNotFound || surnameRange.location != NSNotFound)
{
[self.filteredCVData addObject:person];
}
}
}
[self.collectionView reloadData];
}

Adding NSTableView to NSView Programmatically

I am having a bit of trouble adding a NSTableView to an NSView programatically. The view is the first view of an NSSplitView. My Pointers are set up right i am sure of it because I can add a NSButton to the view no problem. Also my tableview's delegate and datasource methods are working as expected. If I use interface builder to add the table view to my view it works. However, I dont want to use IB. I would like to be able to do this through code. Here is the code I am currently using.
-(void)awakeFromNib{
tableData = [[NSMutableArray alloc]initWithObjects:#"March",#"April",#"May", nil];
tableView = [[NSTableView alloc]initWithFrame:firstView.frame];
[tableView setDataSource:self];
[tableView setDelegate:self];
[firstView addSubview:tableView];
NSButton *j = [[NSButton alloc]initWithFrame:firstView.frame];
[j setTitle:#"help"];
[firstView addSubview:j];
}
The NSButton object appears on screen although if I comment out the button the tableview does not appear. What am I doing wrong. Thanks for the help.
Thank you, from your help I was able to figure this out. IB automatically inserts the NSScrollview around the table view and it also inserts a column for you. In order to do it this from code you need to allocate a scroll view and a column. Here is what I am currently utilizing if anyone else comes across this problem.
-(void)awakeFromNib{
tableData = [[NSMutableArray alloc]initWithObjects:#"March",#"April",#"May", nil];
NSScrollView * tableContainer = [[NSScrollView alloc] initWithFrame:firstView.bounds];
//This allows the view to be resized by the view holding it
[tableContainer setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
tableView = [[NSTableView alloc] initWithFrame:tableContainer.frame];
[tableView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
NSTableColumn *column =[[NSTableColumn alloc]initWithIdentifier:#"1"];
[column.headerCell setTitle:#"Header Title"];
[tableView addTableColumn:column];
[tableView setDataSource:self];
[tableView setDelegate:self];
[tableContainer setDocumentView:tableView];
[firstView addSubview:tableContainer];
//You mentioned that ARC is turned off so You need to release these:
[tableView release];
[tableContainer release];
[column release];
}
Thanks for your help.
NSTableView by default is in NSScrollView. So You can do it like this:
tableData = [[NSMutableArray alloc] initWithObjects:#"March",#"April",#"May", nil];
NSScrollView * tableContainer = [[NSScrollView alloc] initWithFrame:firstView.frame];
tableView = [[NSTableView alloc] initWithFrame:firstView.frame];
[tableView setDataSource:self];
[tableView setDelegate:self];
[tableContainer setDocumentView:tableView];
[firstView addSubview:tableContainer];
//You mentioned that ARC is turned off so You need to release these:
[tableView release];
[tableContainer release];

Changing the Image for a detail disclosure button (Xcode 4.2)

I have used the following suggested routine to change the detail disclosure button image in a table view cell (in tableView cellForRowAtIndexPath)
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
UIButton *myAccessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
[myAccessoryButton setBackgroundColor:[UIColor clearColor]];
[myAccessoryButton setImage:[UIImage imageNamed:#"ball"] forState:UIControlStateNormal];
[cell setAccessoryView:myAccessoryButton];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
However, the event tableView accessoryButtonTappedForRowWithIndexPath is now no longer called on clicking the button.
Has anyone any ideas as to why?
I've +1'd Fry's answer, but just to pull the code in from the other site to make SO more complete: The answer is that you have to accessoryButtonTapped.... call will not be made automatically as it would for the built-in detail-disclosure, so you have to do it yourself with the target of the button.
Translating from the link code to your example above, add this line after setImage:
[myAccessoryButton addTarget:self action:#selector(accessoryButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
Then add this later on:
- (void)accessoryButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil) {
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
(copied verbatim from this link)
Note that this assumes that the button is created in the UITableViewController (in my case I'm creating one in a custom cell so the references are slightly different)
Explanation: the accessoryButtonTapped is a custom target method for our custom button. When the button is pressed ("TouchUpInside") then we find the indexPath of the cell at the point in which the press occurred, and invoke the method that the table view would ordinarily invoke.

Resources