How can I change the background cell permanently after being selected? What I know is by using cell.selectedBackgroundView it will only change the cell background for a while and then it goes back to normal.
I want it to change permanently when selected even after app are closed. I tried to search for a solution but could not find relevant answer.
this is my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView2 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView2 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
//set up selected cell background
cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"white_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
cell.selectedBackgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"blue_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
}
//set up cell
CGFloat nRed= 0/255.0;
CGFloat nGreen=73.0/255.0;
CGFloat nBlue=144.0/255.0;
UIColor *myColor=[[UIColor alloc]initWithRed:nRed green:nGreen blue:nBlue alpha:1];
//set up cell text
cell.textLabel.text = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:#"title"];
cell.textLabel.highlightedTextColor = [UIColor colorWithRed:51.0f/255.0f green:102.0f/255.0f blue:153.0f/255.0f alpha:1.0f];
cell.textLabel.font=[UIFont fontWithName:#"HelveticaNeue-Bold" size:15];
cell.textLabel.textColor=myColor;
cell.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 0.2 alpha: 1.0];
//set icon image for cell
cell.imageView.image = [UIImage imageNamed:
[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:#"icon"]];
return cell;
}
below are my didSelectRowAtIndexPath method;
- (void)tableView:(UITableView *)tableView2 didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
indexPath];
[self->tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:#"action"] isEqualToString:#"a1"]){
NSString *title_a1 =[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:#"title"];
NSString *content_a1 = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:#"content"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// saving an NSString
[defaults setObject:title_a1 forKey:#"title_a1"];
[defaults setObject:content_a1 forKey:#"content_a1"];
WebBrowserViewController *webview=[[WebBrowserViewController alloc]initWithNibName:#"WebBrowserViewController" bundle:nil];
[self presentModalViewController:webview animated:YES];
}else if ([[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:#"action"] isEqualToString:#"a2"]){
NSString *title_a2 =[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:#"title"];
NSString *content_a2 = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:#"content"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// saving an NSString
[defaults setObject:title_a2 forKey:#"title_a2"];
[defaults setObject:content_a2 forKey:#"content_a2"];
WebBrowserViewController *webview=[[WebBrowserViewController alloc]initWithNibName:#"WebBrowserViewController" bundle:nil];
[self presentModalViewController:webview animated:YES];
}else {
NSLog(#" other action key get! ");
}
}
You can add one property to your datasource array, for example, named BOOL isSelected.
in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath you update isSelected = YES. and add the following
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];
to update the selected cell. and in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method
if(data.isSelected){
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"blue_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
}
the code is here:
You need add one bool property to the object in your array. For example,you add
#property (nonatomic) BOOL isSelected; to your object class's header file.
- (UITableViewCell *)tableView:(UITableView *)tableView2 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView2 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
//set up selected cell background
//cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"white_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
//cell.selectedBackgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"blue_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
}
if (((YourObjectType *)[AryStoreknowItem objectAtIndex:indexPath.row]).isSelected){
cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"blue_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
} else {
cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"white_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
}
//set up cell
CGFloat nRed= 0/255.0;
CGFloat nGreen=73.0/255.0;
CGFloat nBlue=144.0/255.0;
UIColor *myColor=[[UIColor alloc]initWithRed:nRed green:nGreen blue:nBlue alpha:1];
//set up cell text
cell.textLabel.text = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:#"title"];
cell.textLabel.highlightedTextColor = [UIColor colorWithRed:51.0f/255.0f green:102.0f/255.0f blue:153.0f/255.0f alpha:1.0f];
cell.textLabel.font=[UIFont fontWithName:#"HelveticaNeue-Bold" size:15];
cell.textLabel.textColor=myColor;
cell.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 0.2 alpha: 1.0];
//set icon image for cell
cell.imageView.image = [UIImage imageNamed:
[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:#"icon"]];
return cell;
}
- (void)tableView:(UITableView *)tableView2 didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
indexPath];
[self->tableView deselectRowAtIndexPath:indexPath animated:YES];
((YourObjectType *)[AryStoreknowItem objectAtIndex:indexPath.row]).isSelected = YES;
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];
if ([[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:#"action"] isEqualToString:#"a1"]){
NSString *title_a1 =[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:#"title"];
NSString *content_a1 = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:#"content"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// saving an NSString
[defaults setObject:title_a1 forKey:#"title_a1"];
[defaults setObject:content_a1 forKey:#"content_a1"];
WebBrowserViewController *webview=[[WebBrowserViewController alloc]initWithNibName:#"WebBrowserViewController" bundle:nil];
[self presentModalViewController:webview animated:YES];
}else if ([[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:#"action"] isEqualToString:#"a2"]){
NSString *title_a2 =[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:#"title"];
NSString *content_a2 = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:#"content"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// saving an NSString
[defaults setObject:title_a2 forKey:#"title_a2"];
[defaults setObject:content_a2 forKey:#"content_a2"];
WebBrowserViewController *webview=[[WebBrowserViewController alloc]initWithNibName:#"WebBrowserViewController" bundle:nil];
[self presentModalViewController:webview animated:YES];
}else {
NSLog(#" other action key get! ");
}
}
Related
I have two UIViewControllers with tableview. When the first cell loads in the second UIViewController it calls the cellForRowAtIndexPath in the same class but when it loads the second cell it calls the first viewControllers cellForRowAtIndexPath.
My code as follows:
SecondViewController:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NotificationsTableViewCell *cell = [self.notificationsTableView dequeueReusableCellWithIdentifier:#"NotificationCell"];
if(cell == nil)
{
cell = [[NotificationsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"NotificationCell"];
}
NSMutableDictionary *cellData = [self.databaseCall transactionFromDatabase:indexPath.row];
NSLog(#"%#", cellData);
cell.goalNameLabel.text = [cellData objectForKey:#"categoryName"];
NSString *cardTypeId = [cellData objectForKey:#"cardTypeId"];
NSString *tipsId = [cellData objectForKey:#"tipsId"];
if([self.defaultCardTypeId containsObject:cardTypeId])
{
NSUInteger index = [self.defaultCardTypeId indexOfObject:cardTypeId];
[self.defaultCardTypeId replaceObjectAtIndex:index withObject:cardTypeId];
}
else{
[self.defaultCardTypeId addObject:cardTypeId];
}
if([self.defaultTipId containsObject:tipsId])
{
NSUInteger index = [self.defaultCardTypeId indexOfObject:cardTypeId];
[self.defaultTipId replaceObjectAtIndex:index withObject:cardTypeId];
}
else{
[self.defaultTipId addObject:tipsId];
}
if([cardTypeId isEqualToString:#"1"])
{
UIImage *cellImage = [UIImage imageNamed:#"icon2.jpg"];
cell.cardTypeImage.image = cellImage;
cell.cardTypeLabel.text = #"GOOD TO KNOW";
cell.cardTypeLabel.textColor = [UIColor colorWithRed:252/255.0 green:171/255.0 blue:19/255.0 alpha:1];
}
if([cardTypeId isEqualToString:#"2"])
{
UIImage *cellImage = [UIImage imageNamed:#"icon1.jpg"];
cell.cardTypeImage.image = cellImage;
cell.cardTypeLabel.text = #"TO CONSIDER";
cell.cardTypeLabel.textColor = [UIColor colorWithRed:0/255.0 green:191/255.0 blue:243/255.0 alpha:1];
}
cell.notificationCard.layer.cornerRadius = 5;
// Configure the cell...
return cell;
}
FirstViewController:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
GoalsCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"GoalsListCell" forIndexPath:indexPath];
if(cell == nil)
{
cell = [[GoalsCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"GoalsListCell"];
}
NSInteger indexOfCategory = [self.databaseCall.arrColumnName indexOfObject:#"CategoryName"];
NSInteger indexOfImage = [self.databaseCall.arrColumnName indexOfObject:#"CategoryImage"];
NSInteger indexOfActive = [self.databaseCall.arrColumnName indexOfObject:#"coulmn"];
//Assigning the contents of cell
cell.goalName.text = [NSString stringWithFormat:#"%#", [[self.arrCategoryTitle objectAtIndex:indexPath.row] objectAtIndex:indexOfCategory]];
NSString *categoryImage = [NSString stringWithFormat:#"%#", [[self.arrCategoryTitle objectAtIndex:indexPath.row] objectAtIndex:indexOfImage]];
NSString *activeStatus = [NSString stringWithFormat:#"%#", [[self.arrCategoryTitle objectAtIndex:indexPath.row] objectAtIndex:indexOfActive]];
UIImage *cellImage = [UIImage imageNamed:categoryImage];
cell.goalImage.image = cellImage;
[cell.favouriteButton addTarget:self action:#selector(favouriteButtonPressed:) forControlEvents:UIControlEventTouchDown];
NSMutableString *selectedRowImage = [[NSMutableString alloc] initWithString:#""];
//Checking whether the category is selected by user or not
if([activeStatus isEqualToString:#"yes"])
{
selectedRowImage = [NSMutableString stringWithFormat:#"starsel.png"];
}
else
{
selectedRowImage = [NSMutableString stringWithFormat:#"stardef.png"];
}
UIImage *favouriteIconImage = [UIImage imageNamed:selectedRowImage];
[cell.favouriteButton setBackgroundImage:favouriteIconImage forState:UIControlStateNormal];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Configure the cell...
return cell;
}
Thanks in advance.
First of all i would say sorry for this stupid question.
The problem is due to the tableview datasource as specifies by #Paulw11, #Onik IV, #Kannan Vora. The secondViewController tableView has the datasource of firstViewController.
What I want to do here is set it so that if top3ArrayForSection.count-1 < 1, set the _results bool value of that respective section to NO, and so on. What's happening instead, is that _results is set to NO or YES for the entire table overall, so that I end up with a result like this:
When only the "xperia Z3 compact unlocked" section should say "no items found etc." because it has no cells, the other sections cells shouldn't.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary *currentSectionDictionary = _matchCenterArray[section];
NSArray *top3ArrayForSection = currentSectionDictionary[#"Top 3"];
if (top3ArrayForSection.count-1 < 1){
_results = NO;
_rowCount = 1;
}
else if(top3ArrayForSection.count-1 >= 1){
_results = YES;
_rowCount = top3ArrayForSection.count-1;
}
return _rowCount;
}
// Cell layout
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Initialize cell
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// No cell seperators = clean design
tableView.separatorColor = [UIColor clearColor];
if (_results == NO) {
// title of the item
cell.textLabel.text = #"No items found, but we'll keep a lookout for you!";
cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
}
else if (_results == YES) {
// title of the item
cell.textLabel.text = _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Title"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
// price of the item
cell.detailTextLabel.text = [NSString stringWithFormat:#"$%#", _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Price"]];
cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];
// image of the item
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Image URL"]]];
[[cell imageView] setImage:[UIImage imageWithData:imageData]];
}
return cell;
}
Your instance variable _results is one-dimensional. You could replace it with an NSArray and store the values individually, or you could change the logic in your code as such:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary *currentSectionDictionary = _matchCenterArray[section];
NSArray *top3ArrayForSection = currentSectionDictionary[#"Top 3"];
return (top3ArrayForSection.count-1 < 1) ? 1 : top3ArrayForSection.count-1;
}
// Cell layout
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Initialize cell
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// No cell seperators = clean design
tableView.separatorColor = [UIColor clearColor];
NSDictionary *currentSectionDictionary = _matchCenterArray[indexPath.section];
NSArray *top3ArrayForSection = currentSectionDictionary[#"Top 3"];
if (top3ArrayForSection.count-1 < 1) {
// title of the item
cell.textLabel.text = #"No items found, but we'll keep a lookout for you!";
cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
}
else {
// title of the item
cell.textLabel.text = _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Title"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
// price of the item
cell.detailTextLabel.text = [NSString stringWithFormat:#"$%#", _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Price"]];
cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];
// image of the item
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Image URL"]]];
[[cell imageView] setImage:[UIImage imageWithData:imageData]];
}
return cell;
}
I am storing checkbox values (true/false) in database. I want to set checkbox checked/unchecked depending on the values stored in database. how to do that ? my code is,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%i",indexPath.row];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:14.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:18.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.frame=CGRectMake(75.0, 50.0, 150.0, 20.0);
cell.textLabel.text=[listArray objectAtIndex:indexPath.row];
NSLog(#"Checked arr size %i",[checkedArr count]);
BOOL checked = [[checkedArr objectAtIndex:indexPath.row] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:#"white_bg.png"] : [UIImage imageNamed:#"tick.png"];
cell.accessoryView = [[UIImageView alloc] initWithImage:image];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL checked = [[checkedArr objectAtIndex:indexPath.row] boolValue];
[checkedArr removeObjectAtIndex:indexPath.row];
[checkedArr insertObject:(checked) ? #"FALSE":#"TRUE" atIndex:indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImage *newImage = (checked) ? [UIImage imageNamed:#"tick.png"] : [UIImage imageNamed:#"white_bg.png"];
cell.accessoryView=[[UIImageView alloc] initWithImage:newImage];
NSLog(#"Val is %i",val);
NSLog(#"selected is %#",[listArray objectAtIndex:indexPath.row]);
}
I am storing those values in database using an array checkedArr.
[dbObj insertQuestData:Question answers:listArray checkVal:checkedArr];
here dbObj is database object, others are values that i am storing to database.
Please let me know how to achieve the same.
The list array and checked array should be mutable array sent to database function which fills up the data.
self.listArray = [NSMutableArray array];
self.checkedArray = [NSMutableArray array];
[dbObj insertQuestData:Question answers:self.listArray checkVal:self.checkedArray];
// [self insertList:self.listArray andChecks:self.checkedArray];
The insertQuestData methods should fill up data like this -
- (void) insertList:(NSMutableArray *)list andChecks:(NSMutableArray *)checks
{
for (int i = 0; i < 100; i++) {
[list addObject:[NSString stringWithFormat:#"item %d", i]];
BOOL checkValue = (i % 4) == 0 ? YES : NO;
[checks addObject:[NSNumber numberWithBool:checkValue]];
}
}
The table datasource methods should be like this - (notice the commented line as correction to your code)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"CellIdentifier";//[NSString stringWithFormat:#"Cell%i",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:14.0];
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:18.0];
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// cell.textLabel.frame=CGRectMake(75.0, 50.0, 150.0, 20.0);
cell.textLabel.text=[self.listArray objectAtIndex:indexPath.row];
NSLog(#"Checked arr size %i",[self.checkedArray count]);
BOOL checked = [[self.checkedArray objectAtIndex:indexPath.row] boolValue];
UIImage *image = (!checked) ? [UIImage imageNamed:#"white_bg.png"] : [UIImage imageNamed:#"tick.png"];
cell.accessoryView = [[UIImageView alloc] initWithImage:image];
return cell;
}
This is what my sample code show on iPhone - [every 4th row is (green)ticked/selected and others are grey/unselected]
Feel free to ask if you have any specific questions / issues you face.
I have a bug.
I want to display a UIImageView on cells at special indexPath.row, but these UIImageView repeat while I scroll.
Exemple: I display my UIImageView on a cell indexPath.row == 0, if I scroll down, I see my UIImageView on the cell at indexPath.row == 8.
Here is my code:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [self getCellContentView:CellIdentifier];
}
UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
UIImageView *imgRead = (UIImageView *)[cell viewWithTag:6];
[cell.contentView insertSubview:imgRead aboveSubview:lblTemp1];
contentDictio = [dict objectAtIndex:indexPath.row];
lblTemp1.text = [contentDictio objectForKey:#"title"];
NSArray *paths_id = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath_id = ([paths_id count] > 0) ? [paths_id objectAtIndex:0] : nil;
NSString *path_id = [basePath_id stringByAppendingPathComponent:#"id.plist"];
NSMutableArray *mut_array_id = [[NSArray arrayWithContentsOfFile:path_id] mutableCopy];
NSMutableDictionary *c0 = [[NSMutableDictionary alloc] init];
NSString *idPlistData = [contentDictio objectForKey:#"id"];
for(c0 in mut_array_id) {
if([[c0 objectForKey:#"id"] isEqualToString:idPlistData]) {
[imgRead setImage:[UIImage imageNamed:#"read"]];
}
else {
}
}
}
return cell;
}
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
CGRect Label1Frame = CGRectMake(kTableCellSmallMargin*2 + 60, kTableCellSmallMargin, 240, 25);
UILabel *lblTemp;
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier] autorelease];
//Initialize Label with tag 1.
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.tag = 1;
lblTemp.backgroundColor = [UIColor clearColor];
[lblTemp setFont: [UIFont fontWithName:#"HelveticaNeue-CondensedBold" size:15.0]];
[cell.contentView addSubview:lblTemp];
[lblTemp release];
UIImageView *read=[[UIImageView alloc] initWithFrame:CGRectMake(kTableCellSmallMargin, kTableCellSmallMargin, 60, 60)];
read.backgroundColor=[UIColor clearColor];
read.tag = 6;
[cell.contentView addSubview:read];
[read release];
return cell;
}
Thanks...
The cell is cached, so you have to clear it when you want no image, like this:
BOOL found = NO;
for(c0 in mut_array_id) {
if([[c0 objectForKey:#"id"] isEqualToString:idPlistData]) {
[imgRead setImage:[UIImage imageNamed:#"read"]];
found = YES;
}
else {
}
}
if (!found)
{
[imgRead setImage:nil];
}
I have a trouble with ios6 and UITableViewCell. The backgroundcolor is not set, this is what it used to work in ios5
-(UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
NSString *contentForThisRow = [[self myArray] objectAtIndex:[indexPath row]];
cell = [tableView dequeueReusableCellWithIdentifier:#"noticeCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"noticeCell"];
}
cell.textLabel.text = contentForThisRow;
id path = [NSString stringWithFormat:#"myip%#.jpeg", IDimage];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
cell.imageView.image = img;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:15.0];
UIColor *cellBackground = [UIColor colorWithRed:235.0/255.0 green:245.0/255.0 blue:255.0/255.0 alpha:0.25];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell setBackgroundColor:cellBackground];
return cell;
}
but now I'm not able to set it again!
How should I do this?
Thanks!
Use
cell.contentView.backgroundColor= [UIColor blueColor];
you may use clear color for cell textlabel background.
It should work.
:)