Tableviewcell how to give alternate cell colour to group of items - objective-c

I have a tableview with a summary page of specific data. In that the data comes in a such a way Example
first row - 1
second row - 1
third row - 1
fourth row - 2
fifth row - 2
sixth row -3
seventh row - 4
How can I give cell background colour grey to all row having 1, red to all row having 2, again grey to all row having 3 and red to all row having 4

If you want the color of the row to change based on some value you can just use cell.backgroundColor to assign it after a check on the value you're interested in. For example let's say that value is in the variable value:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cellIdentifier" forIndexPath:indexPath];
/* your other code */
if (value%2 == 1)
cell.backgroundColor = [UIColor grayColor];
else
cell.backgroundColor = [UIColor redColor];

Related

Position a sheet at a row in NSTableView

I'm trying to position a sheet within an NSTableView just below a certain row. I have the row number and have been successfully able to position the sheet at the right column but not the right row.
Apparently NSRect is drawing up from the bottom of the window containing tableView and I can't figure out how to have it identify the row... I know how to position the sheet at a button or text field NSRect cellRect = [myButton frame]; but not at a row...
Any suggestions or pointers would be very helpful, thanks!
- (NSRect)window:(NSWindow *)window willPositionSheet:(NSWindow *)sheet usingRect:(NSRect)rect {
//the row number is stored as a user default - convoluted but it is what it is
int rowNumber = [[[NSUserDefaults standardUserDefaults] stringForKey:#"p"] intValue];
NSRect cellFrame = [tableView frameOfCellAtColumn:2 row:rowNumber];
cellFrame.size.height = 0;
return cellFrame;
}

Objc how to compare current with previous double values in a tableview cell

I have a tableview that gets refreshed every 10 sec with new double values and i need a mechanism to compare current with previous values but i jam not doing it correctly.
this is what i have now. Can you please propose how to store the current/previous values to get compare after refresh. thx in advance
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierCell forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierCell];
}
Value = [[tempDictionary objectForKey:#"value"] doubleValue];
if (ValueCurrent > ValuePrevious && ValuePrevious !=0.0) {
Label.backgroundColor = [UIColor greenColor];
}
if (ValueCurrent < ValuePrevious) {
Label.backgroundColor = [UIColor redColor];
}
ValuePrevious = Value;
return cell;
}
On my mind all such checks should be performed on model level - in tempDictionary in your case. Add one more key "changed" and set it to #(YES) if new value is different from the previous one when dictionary is updated and to #(NO) otherwise.
Then you can check this value in cellForRowAtIndexPath: and set up background color.
First of all I'm not sure if you need to compare new values of each cell to its previous. If you need to compare new value with previous value respective to each cell then you have to create an NSArray of NSDictionary separately for previous and current values, then compare them each with respective to each cell.
And if you would like to compare only one value when UITableView reloads then you shouldn't compare it in cellForRowAtIndexPath instead compare it when you call [tableView reloadData].
Hope it helps!

How to set color of one row in NSTableView?

Who can help me?
I want make the row to be red, but it is always all row to be red!
-(void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell
forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSColor *cRed = [NSColor redColor];
NSTextFieldCell* cCell = (NSTextFieldCell*)cell;
if ([[tableView selectedRowIndexes] containsIndex:4])
{
[cCell setTextColor:cRed];
}
else
{
[cCell setTextColor:[NSColor blackColor]];
}
}
If any of your selected rows in your table include the 5th row (i.e. index 4), this line in your code:
if ([[tableView selectedRowIndexes] containsIndex:4])
hits and you are seeing red in every row being displayed.
Perhaps if you change that line to:
if(row == 4)
(where row is a parameter passed into that method)
You'll just see one row changed to red.
To be honest, this isn't the best place to set a row/cell's color. I think it would be better to set it in your table view data source (e.g. tableView:objectValueForTableColumn:row:).

Why is UITableViewCell not reloaded properly?

I´m loading a bunch of countries to a table and allowing the user to delete. The rows directly correspond to the countries array, by using objectAtIndex:indexPath.row and I set the delete button´s tag to the same index.
When I delete the first couple of rows it´s ok but afterwards the wrong rows are getting deleted and the app crashes.
Here's how I create rows:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
NSString *currentText;
// fill in country cell content
if ([tableView tag] == 400) {
cell = [tableView dequeueReusableCellWithIdentifier:#"SettingsCountryCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"SettingsCountryCell"];
NSLog(#"is nil");
} // it´s never nil
currentText = [arrSelectedCountries objectAtIndex:indexPath.row];
int currentRow = indexPath.row;
UILabel *countryLabel = (UILabel *)[cell viewWithTag:401];
countryLabel.text = currentText;
// create button
UIButton *countryButton = (UIButton *)[cell viewWithTag:402];
[countryButton setTag:currentRow];
[countryButton setTitle:[NSString stringWithFormat:#"row%d", currentRow]
forState:UIControlStateNormal];
NSLog(#"%# - tag %d - title %# - button tag %d",
currentText,
currentRow,
countryButton.titleLabel.text,
countryButton.tag);
// on load: Bulgaria - tag 2 - title row2 - button tag 2
// after one deletion (and ever after):
// Bulgaria - tag 1 - title (null) - button tag 0
[countryButton addTarget:self
action:#selector(removeCountry:)
forControlEvents:UIControlEventTouchUpInside];
}
Here's how I remove them (i.e. countries):
- (void)removeCountry:(UIButton *)countryButton
{
// removed country
NSString *currentText = [arrSelectedCountries objectAtIndex:countryButton.tag];
NSLog(#"removed country %# at tag %d", currentText, countryButton.tag);
// ok: removed country All CEE Countries at tag 0
// ok: removed country Bulgaria at tag 0
// not ok: removed country Czech Republic at tag 1
// add to picker selection and remove from selected
[arrCountries addObject:currentText];
[arrSelectedCountries removeObject:currentText];
// refresh picker and country table
[countryPicker reloadAllComponents];
[countryTable reloadData];
[countryTable reloadSections:[NSIndexSet indexSetWithIndex:0]
withRowAnimation:UITableViewRowAnimationFade];
// position table and elements below it
[self repositionForCountryChange];
}
At first the table is showing: row0, row1, row2, etc.
After a couple of deletions: row1, row2, row3, etc.
Once this happens, the label is no longer corresponding properly to the tag. When I click delete on Croatia, delete button labeled row 1, NSLog says:
removed country Czech Republic at tag 1
... and Croatia row is not deleted although its delete button now says row2.
And why am I getting from NSLog:
title (null) - button tag 0
...when I can see in the table that there is a title and removeCountry can access button tag, although incorrectly?
You have set up the button with a tag to allow you to identify it as a subview in the cell (402) you then change the tag so it identifies the row the tag is in. When the cell is re-used, there is no view with tag 402, so your button is still identified with a tag number of its original row.
You're using tags for two purposes. Ideally, don't use them at all as they are fragile and do not make your code readable.
create a UITableViewCell subclass and identify its subviews using outlets
identify the row of the pressed button using the method I describe here which is much more flexible than tags and keeps working if you delete rows.

NSTableView + NSTextFieldCell dynamic row size

I want to display a string inside a tableview that can be ( max ) 4000 characters long. I have tried using NSAttributedString to find out the row height for the NSTextFieldCell, but I always fall a bit short when I insert > 4 lines of text.
The NSTextFieldCell is set to using NSLineBreakByWrapping for its line breaks.
What next to NSAttributedString can I use to get the height I need?
id cell = [[tableView tableColumnWithIdentifier:#"test"] dataCell];
[(NSTextFieldCell *)cell setStringValue:[_messages objectAtIndex:row]];
NSRect tallRect = NSMakeRect(0, 0, [tableView frame].size.width, CGFLOAT_MAX);
return [cell cellSizeForBounds:tallRect].height;