Make selected row bold in NSTableView - cocoa

I try to make bold row selection style in NSTableView without any highlighting
I switched highlighting off:
[myTable setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];
However I have some troubles making text bold in selected row. As I suggested, I need to change properties of NSTableView's source:
- (void) tableViewSelectionDidChange: (NSNotification *) notification
{
NSDictionary *boldFont = [NSDictionary dictionaryWithObject:[NSFont boldSystemFontOfSize:13.0]
forKey:NSFontAttributeName];
NSDictionary *normalFont = [NSDictionary dictionaryWithObject:[NSFont systemFontOfSize:13.0]
forKey:NSFontAttributeName];
for(MyClass *obj in tableSourceList)
obj.name = [[NSMutableAttributedString alloc]
initWithString:obj.name
attributes: normalFont];
long row = [myTable selectedRow];
MyClass objectAtSelectedRow = [tableSourceList objectAtIndex: row];
objectAtSelectedRow.name = [[NSMutableAttributedString alloc]
initWithString:dr.dreamname
attributes: boldFont];
[tableSourceList replaceObjectAtIndex:row withObject:objectAtSelectedRow];
}
Unfortunately, there's no effect.
How to make text in row bold when selected?

You can try to achieve by altering the table cell to be rendered in the table's delegate:
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
// Maybe a test on the table column is recommanded if some cells should not be modified
NSIndexSet *selection = [aTableView selectedRowIndexes];
if ([selection containsIndex:rowIndex]) {
[aCell setFont:[NSFont boldSystemFontOfSize:12]];
} else {
[aCell setFont:[NSFont systemFontOfSize:12]];
}
}

Related

TableView selection looks weird if I remove border

I have a very standard NSTableView (this is Mac, not iOS) with a few text rows. Each row is an NSTextField.
I was unhappy with the border of the text fields (see image) so I removed it. However doing so the selected cells look weird.
How can I fix that?
before:
after
Note:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSTextField *cell = [tableView makeViewWithIdentifier:#"MyView" owner:self];
if (cell == nil) {
cell = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, tableView.bounds.size.width, 30)];
cell.identifier = #"MyView";
cell.editable = NO;
cell.bordered = NO;
}
cell.stringValue = [NSString stringWithFormat:#"row %d", row];
return cell;
}
I have tried to set cell.selectable = YES but it didn't help. I have also tried to play with some properties on the storyboard ("focus ring", ...) but it didn't work either. Any idea?
Setting cell.backgroundColor = [NSColor clearColor]; did the trick (transparent background)

NSOutlineView Source List without a NIB

I am trying to figure out how to do a source list 100% in code. I've been hacking away at SideBar demo and various SO questions/answers and have gotten close, but not quite there. I've copied my code below, this seems to work in that it does build a view, with 3 empty cells I can select. What I can't quite figure out is how to get my text to show up in those cells. I will ultimately replace that with my own view, but I'd settle for the stock rendering to work for now..
In my controller:
#implementation SourceListController {
NSScrollView *_scrollView;
NSOutlineView *_sourceList;
SourceListDataSource *_sourceListDataSource;
}
- (void)loadView {
_scrollView = [[NSScrollView alloc] init];
_sourceList = [[NSOutlineView alloc] init];
_sourceListDataSource = [[SourceListDataSource alloc] init];
[_sourceList setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleSourceList];
NSTableColumn *c = [[NSTableColumn alloc] initWithIdentifier: #"Column"];
[c setEditable: NO];
[c setMinWidth: 150.0];
[_sourceList addTableColumn: c];
[_sourceList setOutlineTableColumn:c];
[_sourceList setDelegate:_sourceListDataSource];
[_sourceList setDataSource:_sourceListDataSource];
[_scrollView setDocumentView:_sourceList];
[_scrollView setHasVerticalScroller:YES];
[_sourceList reloadData];
NSLog(_sourceList.dataSource == _sourceListDataSource ? #"YES" : #"NO");
self.view = _scrollView;
}
And in my data source/delegate:
#implementation SourceListDataSource {
}
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
return item == nil ? 3 : 0;
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
return [NSObject new];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return item == nil;
}
- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
NSLog(#"Here %f", tableColumn.width);
NSTableCellView *result = [[NSTableCellView alloc] init];
NSTextField *textField = [[NSTextField alloc] init];
[textField setStringValue:#"Test"];
[result addSubview:textField];
return result;
}
UPDATE: Further digging tells me viewForTableColumn doesn't even get called.. I am stumped as to why..
UPDATE 2: Figured out that it was the missing column and outlineTableColumn leading to not calling the delegate method. I fixed that and updated the code, but still no text shows up.
Your delegate method needs changes, it won't work that way, and also doesn't make use of the new caching methods for views. Mostly, use the NSTableCellView.textFieldproperty instead of adding your own NSTextFieldwithout frame and frame constraints. So, the code that should work looks like this:
- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
NSTableCellView *result = [outline makeViewWithIdentifier:#"MyView" owner:self];
if (result == nil) {
// Create the new NSTextField with a frame of the {0,0} with the width of the table.
// Note that the height of the frame is not really relevant, because the row height will modify the height.
CGRect myRect = CGRectMake(0,0,outlineView.framesize.width,0);
result = [[NSTableCellView alloc] initWithFrame:myRect];
// The identifier of the view instance is set to MyView.
// This allows the cell to be reused.
result.identifier = #"MyView";
}
result.textField.stringValue = #"Test";
return result;
}
All the above typed in SO.

NSTableView column Header text editing not working properly

I want to change the table view column header text on double clicking column header.
For that I created a subclass of NSTableHeaderCell like-
#interface NBETableHeaderCell : NSTableHeaderCell<NSTextFieldDelegate,NSTableViewDelegate>
#end
#import "NBETableHeaderCell.h"
#implementation NBETableHeaderCell
- (void)textDidEndEditing:(NSNotification *)notification
{
NSTextView *editor = notification.object;
[self setTitle:editor.string];
[self setHighlighted:NO];
[self endEditing:editor];
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
if([self isHighlighted])
{
[self drawFocusRingMaskWithFrame:cellFrame inView:controlView.superview];
}
[super drawWithFrame:cellFrame inView:controlView];
}
- (void)drawFocusRingMaskWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
[controlView lockFocus];
NSSetFocusRingStyle(NSFocusRingOnly);
[[NSBezierPath bezierPathWithRect:cellFrame] fill];
[controlView unlockFocus];
}
#end
I set the table column header cell class
-(void)setupTableHeader:(id)table {
NSArray *columns = [table tableColumns];
NSEnumerator *cols = [columns objectEnumerator];
NSTableColumn *col = nil;
NBETableHeaderCell *iHeaderCell;
while (col = [cols nextObject]) {
iHeaderCell = [[NBETableHeaderCell alloc] initTextCell:[[col headerCell] stringValue]];
[col setHeaderCell:iHeaderCell];
[[col headerCell] setEditable:YES];
[iHeaderCell release];
}
[table setTarget:self];
[table setDoubleAction:#selector(doubleClickInTableView:)];
}
The double click action I set was :
- (void)doubleClickInTableView:(id)sender
{
NSInteger row = [myTableView clickedRow];
NSInteger column = [myTableView clickedColumn];
if(row == -1&& column >= 0)
{
NSTableColumn *tableColumn = [[myTableView tableColumns] objectAtIndex:column];
NSTableHeaderView *headerView = [myTableView headerView];
NBETableHeaderCell *headerCell = [tableColumn headerCell];
id cellEditor = [self.view.window fieldEditor:YES forObject:myTableView];
[headerCell setHighlighted:YES];
[headerCell selectWithFrame:[headerView headerRectOfColumn:column]
inView:headerView
editor:cellEditor
delegate:headerCell
start:0
length:headerCell.stringValue.length];
[cellEditor setDrawsBackground:YES];
}
}
Now, I am able to edit the column header and set a new text. On tabbing out the new text persist. But if I set a new text and instead of tabbing out I click on other table column header, my text is not changed, the old value is being retained. This may be because the delegate - (void)textDidEndEditing:(NSNotification *)notification is not called upon selecting other table column header. This delegate is only called on tabbing out. Please suggest, how can set the new value in the table column header cell without tabbing out?

Cocoa : Custom TableView cell?

I am not really familiar with tables, as I usually make games, but now I want to create a level builder where I need a table view with custom cells. I have created a nib file and I have subclassed NSTableCellView, but I don't know what to do next. All I have is:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
NSScrollView * tableContainer = [[NSScrollView alloc] initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)];
SpriteTable *sT = [[SpriteTable alloc]initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)];
NSTableView *tableView = [[NSTableView alloc] initWithFrame: sT.bounds];
NSTableColumn* firstColumn = [[[NSTableColumn alloc] initWithIdentifier:#"firstColumn"] autorelease];
[[firstColumn headerCell] setStringValue:#"First Column"];
[tableView addTableColumn:firstColumn];
tableView.dataSource = self;
tableView.delegate = self;
[tableContainer setDocumentView:tableView];
tableContainer.autoresizingMask = NSViewHeightSizable | NSViewMinXMargin;
[self.window.contentView addSubview: tableContainer];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
return 4;
}
- (NSView *)tableView:(NSTableView *)tableView
viewForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row {
// get an existing cell with the MyView identifier if it exists
CustomCell *result = [tableView makeViewWithIdentifier:#"MyView" owner:self];
// There is no existing cell to reuse so we will create a new one
if (result == nil) {
NSLog(#"result = nil");
// create the new NSTextField with a frame of the {0,0} with the width of the table
// note that the height of the frame is not really relevant, the row-height will modify the height
// the new text field is then returned as an autoreleased object
//result = [[[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 250, 70)] autorelease];
// the identifier of the NSTextField instance is set to MyView. This
// allows it to be re-used
result.identifier = #"MyView";
}
// result is now guaranteed to be valid, either as a re-used cell
// or as a new cell, so set the stringValue of the cell to the
// nameArray value at row
result.imageView.image = [NSImage imageNamed:NSImageNameHomeTemplate];
// return the result.
return result;
}
If any, which delegate methods do I have to implement ? And how do I customize my cell WITH a nib file ?
Do this in ur subview->
#implementation suhasView
#synthesize name,containerView;// container view contains ur subview
- (NSView*) myView
{
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSNib *theNib = [[NSNib alloc] initWithNibNamed:#"suhas"bundle:bundle];
[theNib instantiateNibWithOwner:self topLevelObjects:nil];
return containerView;
}
In Controller->
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
suhas *a=[[suhas alloc]initWithFrame:NSMakeRect(0,0, 40, 40)];
NSView * v = [a myView];
[a.name setStringValue:#"suhas"];
return v;
}...//sorry for my naming of the class:)

iphone, UITextField in UITableView

I'm trying to add textfields in a tableview. I want a label and a textfield in every row except the last row. I want a switch at the last row.
The problem is that the text fields are overlapping on the rest of the rows. I moved my textfield code inside the if(cell == nil) but that didn't work... here's my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *MyIdentifier = #"mainMenuIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
[cell setSelectedBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"highlightstrip.png"]]];
if (tableView.tag == 1) {
UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 90, 20)];
lblName.textAlignment = UITextAlignmentLeft;
lblName.font = [UIFont boldSystemFontOfSize:14];
lblName.backgroundColor = [UIColor clearColor];
lblName.tag = 31;
[cell.contentView addSubview:lblName];
[lblName release];
}
}
if (tableView.tag == 1) {
[(UILabel *) [cell viewWithTag:31] setText:[tableElements objectAtIndex:indexPath.row]];
// check if the last row
if (indexPath.row == 10) {
newsSwtich = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
[newsSwtich addTarget:self action:#selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside];
cell.accessoryView = newsSwtich;
}
else {
UITextField *tempTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 10, 200, 20)];
tempTextField.delegate = self;
// tempTextField.placeholder = [tableElements objectAtIndex:indexPath.row];
tempTextField.font = [UIFont fontWithName:#"Arial" size:14];
tempTextField.textAlignment = UITextAlignmentLeft;
tempTextField.tag = indexPath.row;
tempTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
tempTextField.keyboardType = UIKeyboardTypeDefault; // type of the keyboard
tempTextField.returnKeyType = UIReturnKeyDone; // type of the return key
tempTextField.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
[cell.contentView addSubview:tempTextField];
[tempTextField release];
cell.accessoryView = UITableViewCellAccessoryNone;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
When I scroll up and down the text fields are overlapped , I mean after I enter the text in the first row and scroll down, I can see that textfield copied at the last row as well.
Cell reuse is causing the text fields to overlap. Each time the cell is being reused, you are adding a text field or a switch. These are piling up. You will need to remove the older subview which would either be the switch or the text field before adding one.
why you are using two tableviews just do this
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row==[tableviewarray count]){
//dont add label or textfield
}
else{
// add label or textfield
}
}

Resources