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

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];

Related

NSCollectionView: Go to the next view on selection with Selected Item

I am new to OS X and have started dealing with NSCollectionView. What I am trying to do is to take show NSCollectionView with array of imageView an labels. And on the selection of the image I want to open a new viewController class. I am able to show array of images and labels in collection view but I am completely lost on how to go to new view with the selection made in NSCollectionView and how to show the image selected to the new viewController class.
I am having an NSTabView and in that I am having customView in which I am showing CollectionView. And in awakeFromNib I am doing this to populate my collectionView
-(void)awakeFromNib
{
arrItems = [[NSMutableArray alloc]init];
NSMutableArray *imageArray=[[NSMutableArray alloc]initWithObjects:#"Baby-Girl-Wallpaper-2012-7.jpg",#"Cute-Little-Baby-Girl.jpg",#"Joker_HD_Wallpaper_by_RiddleMeThisJoker.jpg",#"The-Dark-Angel-Wallpaper-HD.jpg",#"hd-wallpapers-1080p_hdwallpapersarena_dot_com.jpg",#"lion_hd_wallpaper.jpg",#"Ganesh_painting.jpg",#"krishna-wallpaper.jpg",#"LeoN_userpic_79630_fire_lion_by_alex_barrera.jpg",#"273483.png",#"japan_digital_nature-wide.jpg", nil];
NSMutableArray *imageName = [[NSMutableArray alloc]initWithObjects:#"Baby-Girl",#"Cute-Little",#"Joker",#"The-Dark",#"hd-wallpapers", #"lion", #"Ganesh", #"krishna", #"LeoN_userpic",#"273483.png",#"japan_digital", nil];
for(int i = 0; i<[imageArray count]; i++)
{
STImageCollectionModal * img1 = [[STImageCollectionModal alloc] init];
img1.image = [NSImage imageNamed:[imageArray objectAtIndex:i]];
img1.imageName = [NSString stringWithFormat:#"%#",[imageName objectAtIndex:i]];
[arrItems addObject:img1];
}
[collectionView setContent:arrItems];
}
Later I created a new class named "STCollectionView" subclass of NSCollectionView and assigned the collectionView class to "STCollectionView" and with the help of setSelectionIndexes method I tried getting the index of the selected item by this
- (void)setSelectionIndexes:(NSIndexSet *)indexes
NSLog(#"%ld",[indexes firstIndex]);
But this method is getting called twice and whenever i put "super setSelectionIndexes" it gives me garbage value.
I am searching it all over but unable to find any kind of solution. Please help..
Thank you in advance.
Your question is confusing me.What i am thinking you can do this task simply by adding UICollectionView and in UICollectionView add custom UICollectionViewCell.In custom UICollectionViewCell add UIImageView and UILabel.
in the method
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"ReuseID";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.imageView.image = [UIImage imagewithNmaed:#"your Image name"];
cell.label.text = #"image Named";
return cell;
}
and in storyBoard hold CLT+drag to your UIViewController.In the method prepareForSgue Method pass data to your Next viewController.

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.

Dynamic text labels in NIB file

I created a nib file and want to display dynamic text messages on it like file names that are selected or the no of files selected etc. Is there a way to to this?
I know this can be done for alert panels but i want it on my custom sheets.
Thanks
Either create connections between your NSTextField elements and your controller class and then set the labels programmatically (using setStringValue).
Or you could consider using bindings. See http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaBindings/CocoaBindings.html.
You can create a NSTextField programmatically like this:
(IBAction)showText:(id)sender {
NSRect frame = NSMakeRect(50, 50, 200, 100);
NSTextField *tf = [[NSTextField alloc] initWithFrame:frame];
[tf setStringValue:#"test"];
[tf setSelectable:NO];
[tf setEditable:NO];
[tf setBordered:NO];
[tf setDrawsBackground:NO];
[[[sender window] contentView] addSubview:tf];
[tf release];
}
or you could use NSString's methods for drawing text in a view, namely -drawAtPoint or -drawInRect

Clickable url link in NSTextFieldCell inside NSTableView?

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

Resources