Redraw issue with UITableView using Objective C and Xcode 6 - ios

I have a tableView which lists student names and highlights the row green for present or red for absent. If you click the row, the color is changed to reflect and change in status for the student.
However, when a student is marked absent (highlighted red) and then I scroll the row out of view the row turns grey. I would love for the row to remain red (ugh).
Here are the tableView methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [kidsNames count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [kidsNames objectAtIndex:(indexPath.row)];
NSString * status=[kidsAbsent objectAtIndex:indexPath.row];
if([status isEqualToString: #"Present"]==1){
cell.contentView.backgroundColor = [UIColor greenColor];
}else{
cell.contentView.backgroundColor = [UIColor redColor];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString * ab=[kidsAbsent objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(#"CLICKED!");
if([ab isEqualToString: #"Present"]==1){
[kidsAbsent replaceObjectAtIndex:indexPath.row withObject:#"Absent"];
cell.contentView.backgroundColor = [UIColor redColor];
NSLog(#"Now Absent %#",[kidsNames objectAtIndex:(indexPath.row)]);
}else{
cell.contentView.backgroundColor = [UIColor greenColor];
[kidsAbsent replaceObjectAtIndex:indexPath.row withObject:#"Present"];
NSLog(#"Now Present %#",[kidsNames objectAtIndex:(indexPath.row)]);
}
stuID=[kidsID objectAtIndex:indexPath.row];
stuAB=[kidsAbsent objectAtIndex:indexPath.row];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString * ab=[kidsAbsent objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(#"CLICKED!");
if([ab isEqualToString: #"Present"]==1){
[kidsAbsent replaceObjectAtIndex:indexPath.row withObject:#"Absent"];
cell.contentView.backgroundColor = [UIColor redColor];
NSLog(#"Now Absent %#",[kidsNames objectAtIndex:(indexPath.row)]);
}else{
cell.contentView.backgroundColor = [UIColor greenColor];
[kidsAbsent replaceObjectAtIndex:indexPath.row withObject:#"Present"];
NSLog(#"Now Present %#",[kidsNames objectAtIndex:(indexPath.row)]);
}
//send info to the web===============================================================================
stuID=[kidsID objectAtIndex:indexPath.row];
stuAB=[kidsAbsent objectAtIndex:indexPath.row];
}

Try to set cell.backgroundView not cell.contentView.backgroundColor
Or use cell.contentView.backgroundColor but you should note that the backgroundColor must be set in the tableView:willDisplayCell:forRowAtIndexPath: method (from UITableViewCell reference):

Related

Adding disclosure indicator to tableview cell

I believe I'm doing this correctly but it doesn't seem to be working? The text for the cell is written indicating the if statement is correct and is running, However the cell accessory is not changing.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (indexPath.section==0) {
if ([[tableContents objectAtIndex:indexPath.row] isEqual: #"New Time"]) {
cell.tintColor = [UIColor redColor];
cell.textLabel.text = [tableContents objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else{
cell.textLabel.text = [tableContents objectAtIndex:indexPath.row];
}
}
else{
cell.textLabel.text = [tableContents objectAtIndex:indexPath.row];
}
return cell;
}
Thanks for any help in advance!
Okay so tried slimming the code down to try and see whats going on, now this crashes when the view is loaded, totally confused as i have another project where this is working perfectly!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"UserCells";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = #"Hello";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Setting the cell tint color in cellForRowAtIndexPath did work:
cell.tintColor = [UIColor redColor]; // whatever you want

UITableViewCellSelectionStyleBlue is not working

I need to use UITableViewCellSelectionStyleBlue for the cell selection style. But whatever style I set it only displays UITableViewCellSelectionStylegray. I am using Xcode 5.
This is my code:
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellid = #"Table1";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (cell==Nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
}
cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];
cell.selectionStyle =UITableViewCellSelectionStyleBlue;
cell.imageView.image =[UIImage imageNamed:#"cellbgimg.png"] ;
return cell;
}
i am able to change the UITableviewSelectionStyleBlue. Please try below code:
// take a view for the cell...
UIView *cellBg = [[UIView alloc] init];
cellBg.backgroundColor = [UIColor colorWithRed:(76.0/255.0) green:(161.0/255.0) blue:(255.0/255.0) alpha:1.0]; // this RGB value for blue color
cellBg.layer.masksToBounds = YES;
Cell.selectedBackgroundView = cellBg;
Try this one ....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if (cell == nil)
{
cell = [self tableviewCellWithReuseIdentifier:#"myCell" ForIndexpath:indexPath];
}
[self configureCell:cell forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue]; // This one for your cell selection style.
return cell;
}

Accessory type for Cross Sign in iOS

I have a table view and 2 custom cells inside it. I want to use a (X) symbol i.e. the cross symbol and the tick symbol. I know for tick we have UITableViewCellAccessoryCheckmark but what to do for the (X) symbol. Can we have any custom accessory for this or else how to achieve this?
My code:
#pragma mark - Table View Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
optionsLabelArray = [[NSArray alloc] initWithObjects:#"Yes", #"No", nil];
static NSString *CellIdentifier = #"CheckerCell";
CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.optionLabel.text = [optionsLabelArray objectAtIndex:indexPath.row];
cell.optionLabelSubtitle.text = [optionsLabelSubtitleArray objectAtIndex:indexPath.row];
if(indexPath.row == 0)
{
cell.textLabel.textColor = [UIColor colorWithRed:78/255.0 green:157/255.0 blue:19/255.0 alpha:1.0];
}
else if(indexPath.row == 1)
{
cell.textLabel.textColor = [UIColor colorWithRed:167/255.0 green:19/255.0 blue:43/255.0 alpha:1.0];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView ==_optionsTableView1) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
previousSelectedCell1.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryType=UITableViewCellAccessoryCheckmark;
previousSelectedCell1 = cell;
NSLog(#"Row : %ld", (long)indexPath.row);
if(indexPath.row == 0)
{
self.weatherSafeToPlay = YES;
}
else
{
// MatchDayDataController *sharedController = [MatchDayDataController sharedDataController];
// sharedController.actions = [sharedController.actions stringByAppendingString:#"Check Weather. "];
//[sharedController.actions appendString:#"Check Weather. "];
self.weatherSafeToPlay = NO;
}
NSLog(#"Is Weather safe: %hhd", self.weatherSafeToPlay);
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
not heard of any accessory type for cross but in such cases, i simply use unicode characters.
It's lighter than using an image and is a simple matter of just changing the text.
#"\u2713" for ✓
#"\u2717" for ✗
You can:
Create a custom UITableViewCell having a 32by32 (or any dimension) UILabel (on the right/left/middle/wherever)
in -didSelectRowAtIndexPath, you can change the text of this UILabel
#"\u2713" for checkmark or #"\u2717" for crossmark
A simple example (on the default UITableViewCell's default textLabel, just for the idea):
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if([cell.textLabel.text isEqualToString:#"\u2713"]) {
cell.textLabel.text = #"\u2717";
}
else {
cell.textLabel.text = #"\u2713";
}
}
OR... when not using a custom cell, something like:
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
//set your text to cell.textLabel.text
//create your own UILabel
UILabel *lblAcc = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 32, 32)];
[lblAcc setTag:100];
[lblAcc setText:#"?"];
[lblAcc setBackgroundColor:[UIColor redColor]];
[lblAcc setTextAlignment:NSTextAlignmentCenter];
//set as custom AccessoryView on cell
[cell setAccessoryView:lblAcc];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *strAccessory = [(UILabel *) [cell viewWithTag:100] text];
//the -isEqualToString is a bit heavier than checking a simple bool or int value
//so since you should be remembering the selected cells anyways, you can change
//the if-logic to compare with a bool rather than the string comparison.
//but for now, we'll do:
if([strAccessory isEqualToString:#"\u2713"]) {
[(UILabel *) [cell viewWithTag:100] setText:#"\u2717"];
}
else {
[(UILabel *) [cell viewWithTag:100] setText:#"\u2713"];
}
}
for more unicode characters... this link:
http://en.wikibooks.org/wiki/Unicode/List_of_useful_symbols
You can assign your own image to accessoryView of table cell. Create an image of (X) symbol and use that image as accessoryView.
cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"Your X Image"]];

Data repeat in UITableView when scrolling

I use bellow codes to show data on TableView but when scrolling, data repeat and other data lost.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [FileCompletedArray count];
}
cellForRowatindexpath ():
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *FileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
FileNameLabel.backgroundColor = [UIColor clearColor];
FileNameLabel.font = [UIFont fontWithName:#"Helvetica" size:16];
FileNameLabel.font = [UIFont boldSystemFontOfSize:16];
FileNameLabel.textColor = [UIColor blackColor];
NSLog(#"Reseversed TEMP array %#",FileCompletedArray);
FileNameLabel.text =[FileCompletedArray objectAtIndex:indexPath.row];
[cell.contentView addSubview: FileNameLabel];
[FileNameLabel release];
}
return cell;
}
Do you have any solutions? Thanks in advance
use cell Identifier different
For example like bellow...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"%d,%d",indexPath.section,indexPath.row];
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
/// write your code here..
}
}
OR set nil like bellow..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
/// write your code here..
}
}
This problem is generated because Your UITableView Reuse Cell instant of create new one.
I give you some suggestion that may be helpful for you.
1) add Controller in between
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
/// Your code of initialization of controller;
}
/// set property of controllers such like (Text UILabel, image of UIImageView...etc) .
return cell;
}
1) Add Your Controller to cell.contentView.
EDITED:
Before You follow my Edited Answer i want to tell you that following code is bad for memory management because it will create new cell for each rows of UITableView, so be careful for it.
But it is better for if UITableView Have Limited row about (50-100 may be ) os use following code if is it suitable for you.
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
/// Your whole code of controllers;
}
You are setting the text of the label only when creating a new cell. However, during cell reuse, new cell is not created. Hence your code for setting/changing text is not working. You can use this modified code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *FileNameLabel=nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
FileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
FileNameLabel.tag = 1001;
FileNameLabel.backgroundColor = [UIColor clearColor];
FileNameLabel.font = [UIFont fontWithName:#"Helvetica" size:16];
FileNameLabel.font = [UIFont boldSystemFontOfSize:16];
FileNameLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview: FileNameLabel];
[FileNameLabel release];
}
if(!FileNameLabel)
FileNameLabel = [cell.contentView viewWithTag:1001];
FileNameLabel.text =[FileCompletedArray objectAtIndex:indexPath.row];
return cell;
}
Alternatively you can use the default textLabel instead of creating and adding new label
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *FileNameLabel=nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:16];
cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
cell.textLabel.textColor = [UIColor blackColor];
}
cell.textLabel.text =[FileCompletedArray objectAtIndex:indexPath.row];
return cell;
}
I also faced same problem, the problem occurs when we reuse the cells. At the time of reusing the data is already present in it and we write some more data also, to sort out this, i did a minor change to my table view delegate method cellForRowAtIndexPath. Here the code which i use always using table view.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
}
return cell;
}
The for loop gets executed when we are reusing some cell and what it does is removing all the previous data present in UITableViewCell
Hope this will help someone.

uitableview first and last visible cell color change

I'm trying to to implement this function in UITableView for iPhone: always have only the FIRST and LAST VISIBLE cell have a different background color, say red, while color of the other cells remain white. and a smooth change during scrolling.
I have tried:
in .m file:
NSIndexPath *firstRow;
UITableViewCell* firstCell;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"tableCell";
tableCell *cell = (tableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"tableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
//cell.thumbnailImageView.image = image;
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSArray *visible = [tableView indexPathsForVisibleRows];
firstRow = (NSIndexPath *)[visible objectAtIndex:0];
firstCell = [tableView cellForRowAtIndexPath:firstRow];
firstCell.contentView.backgroundColor=[UIColor redColor];
NSLog(#"main visible cell's row: %i", firstRow.row);
[tableView endUpdates];
firstCell.contentView.backgroundColor=[UIColor redColor];
}
But the color wont update when scrolling back up.
You can do it all in cellForRowAtIndexPath if you want.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *tableViewCellID = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewCellID];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellID];
[[cell textLabel] setText:[NSString stringWithFormat:#"some sting %i", [indexPath row]]];
NSArray *visibleCells = [tableView visibleCells];
for (int i = 0; i < [visibleCells count]; i++) {
UITableViewCell *cell = [visibleCells objectAtIndex:i];
if (i == 0 || i == [visibleCells count] - 1)
[cell setBackgroundColor:[UIColor redColor]];
else
[cell setBackgroundColor:[UIColor clearColor]];
}
return cell;
}
In your
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Try something like this
//Find the first row
if(indexPath.row == 0){
cell.contentView.backgroundColor = [UIColor redColor];
}
And to check for the last row, you should reuse the code you made for
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

Resources