TableView selection looks weird if I remove border - cocoa

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)

Related

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

Make selected row bold in NSTableView

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

SDWebImage and UITableViewCell

I'm working with SDWebImage and UITableView
- (UITableViewCell *)tableView:(UITableView *)the_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *MyIdentifier = [NSString stringWithFormat:#"MyIdentifier"];
NSDictionary *info = [tableData objectAtIndex:indexPath.row];
UITableViewCell *cell = [the_tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier] autorelease];
if(!addImage) [addImage release];
addImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"placeholder.png"]];
[addImage setFrame:CGRectMake(7, 10, 50, 50)];
[cell setIndentationLevel:6];
[cell.contentView addSubview:addImage];
}
if(info != NULL) {
cell.textLabel.text = [info objectForKey:#"title"];
cell.detailTextLabel.text = [NSString stringWithFormat:#"Version: %#",[info objectForKey:#"version"]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSString *imageURL = [NSString stringWithFormat:#"%#",[info objectForKey:#"imageURL"]];
[addImage setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
}
return cell;
}
Which Works Great for the first 6 Results (The amount that can fit on the immediate view)
But as I scroll down the list, it seems like it's just re-using images from the first 6 cells, and on some cells images change depending on their location on the screen.
Also, if I call reloadData, the images from the previous UITableCells stay on screen!
Am I doing something wrong here? I've followed the example code on github..
Thanks!
(Answered by the OP in a question edit. Moved here as a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
OK, So I found the problem.
For anyone else out there with the Same issue, this is where you're doing it wrong!
Notice how I'm adding the Image into SubView while creating a cell:
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier] autorelease];
addImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"placeholder.png"]];
[addImage setFrame:CGRectMake(7, 10, 50, 50)];
[cell.contentView addSubview:addImage];
Well, what SDWebImage was trying to do is constantly update that addImage variable, which wasn't a property of my cell class.
So, what I did to fix this problem is create my own UITableViewCell subclass, that init's with a ImageView and I get SDWebImage to setImage on that property!
I hope this helps someone!

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
}
}

xcode uitableview cell image align

I'm sure this is very simple but i can't find any documentation about it..
in my uitableview i have the text labels aligned to the right (my app is in heberw which is rtl)
i'm trying to add an image to the cell to the RIGHT of the text label, no luck, so far the image is aligned to the left and i can't find a way to align it to the right side of the cell.
here's my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.imageView.image = [UIImage imageNamed:#"tableicon.png"];
NSString *cellValue = [[stories objectAtIndex:indexPath.row] objectForKey:#"ArticleTitle"];
cell.textLabel.text = cellValue;
cell.textLabel.textAlignment = UITextAlignmentRight;
cell.textLabel.font = [UIFont systemFontOfSize:17];
cell.textLabel.numberOfLines = 2;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
Instead of adding the image as a subview of your UITableViewCell, add a container with right aligned subviews.
UIImageView *pic = myImageView;
UIView *picContainer = [[UIView alloc] initWithFrame:cell.frame];
picContainer.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
picContainer.contentMode = UIViewContentModeRight;
pic.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
pic.center = CGPointMake(picContainer.frame.size.width-70, picContainer.frame.size.height/2);
[picContainer addSubview:pic];
[cell.contentView addSubview:picContainer];
This has the added benefit of automatically readjusting when the orientation changes.
try this one
cell.accessoryView=[UIImage imageNamed:#"tableicon.png"];

Resources