I am adding image in tableVIew cell but it does not show.
I am using if condition to give the image to button but it does not show.
Here is the code for the button addding
addButton = [UIButton buttonWithType:UIButtonTypeCustom];
addButton.Frame=CGRectMake(590,2,42,42);
[addButton addTarget:self action:#selector(tab1Action:) forControlEvents:UIControlEventTouchUpInside];
addButton.tag=indexPath.row;
NSString*test=theData.Status;
if ([test isEqualToString:#"1"]) {
test=#"Already Member of the group";
}
else {
test=#"";
NSLog(#"Else is also Working Fine");
[addButton setImage:[UIImage imageNamed:#"addbutton.png"] forState:UIControlStateNormal];
}
addButton.clipsToBounds=YES;
[[cell contentView] addSubview:addButton];
cell.textLabel.font=[UIFont fontWithName:#"Helvetica" size:16];
cell.textLabel.text=[NSString stringWithFormat:#"%#: %#",theData.GroupTitle,test];
return cell;
You don't need to add to cell's contentview. So do like this..
[cell addSubview:addButton];
your button frame CGRectMake(590,2,42,42) is the error
frame.origin.x is too big than screen size
it is only on size 320,480 in developing
640,960 is automatic
// Create a table for different section of app
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
/* IMPORTANT: As we are using a "Dynamic Type", when table view reload we must remove previous
views from container.
*/
if ([cell.contentView subviews]){
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
}
// create a rectangle box for image view
UIImageView *iconImage = [[UIImageView alloc] init];
iconImage.frame = CGRectMake(12,4,53.5,53.5);
// Create a label for cells
UILabel *cellName = [[UILabel alloc] initWithFrame:CGRectMake(75,18,200,20)];
cellName.font = [self fontForBodyTextStyle];
NSString *cellNameStr = [NSString stringWithFormat:#"%#",self.tableCellNames[indexPath.row]];
// Select path row tab actions
switch (indexPath.row) {
case 0:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"condition.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 1:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"videos.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 2:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"provider.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 3:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"info.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
default:
NSAssert(NO, #"Unhandled value in cellForRowAtIndexPath");
break;
}
return cell;
}
Thanks VKJ,
Related
I'm creating a UISwitch programmatically in one of the tableview datasource function(cell for row at index). when I'm scrolling the table view, the OFF state switches are malfunctioned(UI) two rounds appeared. Attaching the screenshot of the switch.
Appreciate your help!
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UILabel *title = (UILabel*)[cell viewWithTag:80];
UILabel *description = (UILabel*)[cell viewWithTag:81];
UIView *innerView = (UIView *)[cell viewWithTag:82];
innerView.layer.borderColor=[[UIColor colorWithRed:229/255.0f green:229/255.0f blue:229/255.0f alpha:1.0] CGColor];
innerView.layer.borderWidth = 2.0f;
NSDictionary *displayDict = scenarioListsArray[indexPath.row];
title.text =[displayDict objectForKey:#"name"];
description.text = [displayDict objectForKey:#"description"];
UISwitch *myswitch = [[UISwitch alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-60, (cell.contentView.frame.size.height/2)-20 , 100, 100)];
myswitch.onTintColor = [UIColor colorWithRed:25/255.0f green:122/255.0f blue:66/255.0f alpha:1];
[cell.contentView addSubview:myswitch];
myswitch.tag = indexPath.row;
[myswitch addTarget:self action:#selector(cellButtonClickAction:) forControlEvents:UIControlEventValueChanged];
if ([[displayDict objectForKey:#"status"] isEqualToString:#"ACTIVE"]) {
[myswitch setOn:YES animated:YES];
}
else
{
[myswitch setOn:NO animated:YES];
}
return cell;
}
I have faced the same issue and fixed with following code before creating switch
for(UIView *subView in cell. contentView.subviews){
if ([subView isKindOfClass:[UISwitch class]]) {
[subView removeFromSuperview];
}
}
Or
static NSString *TableIdentifier = #"YourCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifier];
//place your all UI elements code here.
}
I have updated your code, try this will work for you,
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UILabel *title = (UILabel*)[cell viewWithTag:80];
UILabel *description = (UILabel*)[cell viewWithTag:81];
UIView *innerView = (UIView *)[cell viewWithTag:82];
innerView.layer.borderColor=[[UIColor colorWithRed:229/255.0f green:229/255.0f blue:229/255.0f alpha:1.0] CGColor];
innerView.layer.borderWidth = 2.0f;
NSDictionary *displayDict = scenarioListsArray[indexPath.row];
title.text =[displayDict objectForKey:#"name"];
description.text = [displayDict objectForKey:#"description"];
UISwitch *myswitch = [cell.contentView viewWithTag:indexPath.row+597];
if(mySwitch == nil){
myswitch = [[UISwitch alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-60,(cell.contentView.frame.size.height/2)-20 , 100, 100)];
[cell.contentView addSubview:myswitch];
[myswitch addTarget:self action:#selector(cellButtonClickAction:) forControlEvents:UIControlEventValueChanged];
}
myswitch.onTintColor = [UIColor colorWithRed:25/255.0f green:122/255.0f blue:66/255.0f alpha:1];
myswitch.tag = indexPath.row+597; //597 is extra number added to tag
if ([[displayDict objectForKey:#"status"] isEqualToString:#"ACTIVE"]) {
[myswitch setOn:YES animated:YES];
}
else
{
[myswitch setOn:NO animated:YES];
}
return cell;
}
I have a custom table view in my app which has button in each row.
I want to change button background image when flag of that cell is i'1'(check if statement in code.).
it is successfully executing if statements.
Just the button's background image is not changing.
Here is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"cell"];
cell= [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"]autorelease];
if(tableView == tablePatchFilter)
{
cell.textLabel.text=[filterPatchName objectAtIndex:indexPath.row];
}
else
{
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=[displayItems objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel *typeLabel = [[[UILabel alloc]init]autorelease];
typeLabel.backgroundColor = [UIColor clearColor];
typeLabel.text = [displayItemsType objectAtIndex:indexPath.row];
typeLabel.textAlignment = NSTextAlignmentLeft;
typeLabel.font=[UIFont fontWithName:MyFont size:17.f];
typeLabel.frame = CGRectMake(300,2.0f,250,50);
[cell addSubview:typeLabel];
UILabel *typeLabel1 = [[[UILabel alloc]init]autorelease];
typeLabel1.backgroundColor = [UIColor clearColor];
typeLabel1.text = [displayItemPatch objectAtIndex:indexPath.row];
typeLabel1.textAlignment = NSTextAlignmentLeft;
typeLabel1.font=[UIFont fontWithName:MyFont size:17.f];
typeLabel1.frame = CGRectMake(550,2.0f,250,50);
[cell addSubview:typeLabel1];
UILabel *typeLabel2 = [[[UILabel alloc]init]autorelease];
typeLabel2.backgroundColor = [UIColor clearColor];
typeLabel2.font=[UIFont fontWithName:MyFont size:17.f];
typeLabel2.text = [displayItemclass objectAtIndex:indexPath.row];
typeLabel2.textAlignment = NSTextAlignmentLeft;
typeLabel2.frame = CGRectMake(730,2.0f,250,50);
[cell addSubview:typeLabel2];
int selectedSegment = mainSegment.selectedSegmentIndex;
if(selectedSegment == 0)
{
for(int i = 0;i<[displayFlag count];i++)
{ UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag=indexPath.row;
[btn addTarget:self
action:#selector(takeSignature:)
forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
NSMutableString *pathhh= [NSMutableString stringWithFormat:#"%#",[displayFlag objectAtIndex:i]];
NSLog(#"XXXXXXXXXXXXXXXXXXXXXXXXXX%#",pathhh);
if([pathhh isEqualToString:#"1"])
{
NSLog(#"in IFFFFFFF%#",pathhh);
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"signature.png"]
forState:UIControlStateNormal];
}
else
{
NSLog(#"in ELSSSSSSSSSS %#",pathhh);
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"add.png"]
forState:UIControlStateNormal];
}
}
/*
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"signature.png"]
forState:UIControlStateNormal];
[btn addTarget:self
action:#selector(takeSignature:)
forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
*/
}
}
cell.textLabel.font=[UIFont fontWithName:MyFont size:17.f];
return cell;
}
In your implementation every time you are creating new cell, after getting cell from "dequeueReusableCellWithIdentifier" you have to check for cell if it nil than only create new cell using "alloc-init".
The better implementation of the particular senireo is to take a custom UITableViewCell.
The following stategie you should follow.
In the custom cell create the outlet for label 1, 2 and 3, than set the test in cellForRowatIndexPath.
Create a property of NSMutableArray in the custom cell class.
After getting the cell from "dequeueReusableCellWithIdentifier" check if cell is nil than only create the buttons and add all the buttons in the NSMutableArray created in step 2.
take the buttons from the NSMutableArray and that apply the condition for setting the images.
Please see the below code:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView == tablePatchFilter)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.textLabel.text=[filterPatchName objectAtIndex:indexPath.row];
return cell;
} else {
CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
int selectedSegment = mainSegment.selectedSegmentIndex;
if(selectedSegment == 0) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag=indexPath.row;
[btn addTarget:self action:#selector(takeSignature:) forControlEvents:UIControlEventTouchUpInside];
[cell.buttonArray addObject:btn];
}
}
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=[displayItems objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.label1.text = [displayItemsType objectAtIndex:indexPath.row];
cell.label2.text = [displayItemPatch objectAtIndex:indexPath.row];
cell.label3.text = [displayItemclass objectAtIndex:indexPath.row];
for (UIButton *btn in cell.buttonArray) {
NSMutableString *pathhh= [NSMutableString stringWithFormat:#"%#",[displayFlag objectAtIndex:i]];
if([pathhh isEqualToString:#"1"]) {
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"signature.png"] forState:UIControlStateNormal];
} else {
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
}
}
return cell;
}
}
I have two sections in my tableview(iOS), where the first one is for the "favorites" rows. If you select the accessoryView it should add/remove the selected object in the "objects" array to/from "favs" array depending if the object already exist in the section.
The method I wrote almost works, but when I add a second row, or the same to remove it in favorites, the app crashes with this error:
Terminating app due to uncaught exception NSRangeException', reason: *** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]
The problem is that the index of the object in the corresponding arrays is not the same so I can't really figure out how to make this work properly.
Here's some code:
-(void)addToFavs:(id)sender{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSLog(#"Tag = %d", gesture.view.tag);
//if favorite section is empty
if ([self.favs isEqualToArray:[#[]mutableCopy]]) {
NSLog(#"adding favorite");
[self.favs addObject:[self.objects objectAtIndex:gesture.view.tag]];
[self.subtitlesFavs addObject:[self.subtitles objectAtIndex:gesture.view.tag]];
[self.iconsFavs addObject:[self.icons objectAtIndex:gesture.view.tag]];
}
//if selected row already exist in favorites array <--HERE IS THE PROBLEM (I THINK)
else if([[self.objects objectAtIndex:gesture.view.tag] isEqualToString:[self.favs objectAtIndex:gesture.view.tag]]){
NSLog(#"removing favorite");
[self.favs removeObjectAtIndex:gesture.view.tag];
[self.subtitlesFavs removeObjectAtIndex:gesture.view.tag];
[self.iconsFavs removeObjectAtIndex:gesture.view.tag];
}else{
NSLog(#"adding favorite");
[self.favs addObject:[self.objects objectAtIndex:gesture.view.tag]];
[self.subtitlesFavs addObject:[self.subtitles objectAtIndex:gesture.view.tag]];
[self.iconsFavs addObject:[self.icons objectAtIndex:gesture.view.tag]];
}
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// left image
UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(7, 7, 30, 30)];
[image.layer setCornerRadius:image.frame.size.width/2];
[image setClipsToBounds:YES];
if(indexPath.section==0){
image.image=[UIImage imageNamed:[self.iconsFavs objectAtIndex:indexPath.row]];
}else{
image.image=[UIImage imageNamed:[self.icons objectAtIndex:indexPath.row]];
}
//fav image
UIImageView *fav = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"favorites.png"]];
[fav setFrame:CGRectMake(0, 0, 25, 25)];
[fav setClipsToBounds:YES];
if(!indexPath.section==0) {
fav.image=[UIImage imageNamed:#"unfavorites"];
}
//cell
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=self.objects[indexPath.row];
cell.detailTextLabel.text =self.subtitles[indexPath.row];
[cell.contentView addSubview:image];
cell.accessoryView = fav;
//Favorites
cell.accessoryView.userInteractionEnabled = YES;
cell.accessoryView.tag = indexPath.row;
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(addToFavs:)];
tapped.numberOfTapsRequired = 1;
[cell.accessoryView addGestureRecognizer:tapped];
//favorites section contents
if (indexPath.section == 0)
{
cell.textLabel.text=self.favs[indexPath.row];
[cell.contentView addSubview:image];
cell.detailTextLabel.text =self.subtitlesFavs[indexPath.row];
}
}
return cell;
}
Update 1
I edited my code with "isEqualToString" in the if-condition but it still doesn't work to remove them...
Quite similar to what you have but try this anyways:
this be the -cellForRowAtIndexPath: method --
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}
cell.detailTextLabel.textColor = [UIColor lightGrayColor];
//following commented line not needed
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// left image
UIImageView *image = [[UIImageView alloc] init];
[image setFrame:CGRectMake(7, 7, 30, 30)];
[image.layer setCornerRadius:image.frame.size.width/2];
[image setClipsToBounds:YES];
[image.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[image.layer setBorderWidth:0.3f];
[cell.contentView addSubview:image];
//favorites image button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 25, 25)];
[button setBackgroundColor:[UIColor clearColor]];
[button setImage:[UIImage imageNamed:#"unfavorites.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"favorites.png"] forState:UIControlStateSelected];
[button setTag:100]; //tag needed later
[button addTarget:self action:#selector(addToFavs:event:) forControlEvents:UIControlEventTouchUpInside];
[button setShowsTouchWhenHighlighted:YES];
[cell setAccessoryView:button];
switch(indexPath.section) {
case 0: { //is first section (favourite)
image.image = [UIImage imageNamed: self.iconsFavs[indexPath.row]];
cell.textLabel.text = self.favs[indexPath.row];
cell.detailTextLabel.text = self.subtitlesFavs[indexPath.row];
[button setSelected:YES];
}
break;
case 1: { //is second section (all clubs)
image.image = [UIImage imageNamed: self.icons[indexPath.row]];
cell.textLabel.text = self.objects[indexPath.row];
cell.detailTextLabel.text = self.subtitles[indexPath.row];
//change state of button (thereby change button image)
if([self.favs containsObject: self.objects[indexPath.row]]) {
[button setSelected:YES];
} else {
[button setSelected:NO];
}
}
break;
}
//separator -- do the following on viewDidLoad instead
//[self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 45, 0, 0)];
return cell;
}
this be the -addToFavs:event: method --
//No need for `-accessoryButtonTappedForRowWithIndexPath:` method
//it can be done in your following `-addToFavs:event:` method alone:
-(void)addToFavs:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
switch(indexPath.section) {
case 0: { //is first section (favourite)
//remove from favourites
//get indexPath for appropraite row in second section
NSUInteger i_indexOfFavInMain = [self.objects indexOfObject:self.favs [indexPath.row]];
NSIndexPath *indexOfFavInMain = [NSIndexPath indexPathForItem:i_indexOfFavInMain inSection:1];
[self.favs removeObjectAtIndex:indexPath.row];
[self.subtitlesFavs removeObjectAtIndex:indexPath.row];
[self.iconsFavs removeObjectAtIndex:indexPath.row];
//handle second section button (for the appropriate row)
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexOfFavInMain];
UIButton *btnTemp = (UIButton *)[cell viewWithTag:100];
[btnTemp setSelected:NO];
//reload first section
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
}
break;
case 1: { //is second section (all clubs)
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIButton *btnTemp = (UIButton *)[cell viewWithTag:100];
if([self.favs containsObject:self.objects[indexPath.row]]) {
//remove from favourites
[self.favs removeObject:self.objects[indexPath.row]];
[self.subtitlesFavs removeObject:self.subtitles[indexPath.row]];
[self.iconsFavs removeObject:self.icons[indexPath.row]];
[btnTemp setSelected:NO];
} else {
//add to favourites
[self.favs addObject: self.objects[indexPath.row]];
[self.subtitlesFavs addObject: self.subtitles[indexPath.row]];
[self.iconsFavs addObject: self.icons[indexPath.row]];
[btnTemp setSelected:YES];
}
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
}
break;
}
}
Here's the solution. Moving everything to accessoryButoonTappedForRowWithIndexPath and having four different cases did the trick. Also make sure to not have strings that are the same in the arrays otherwise it will remove all of them and you will get an index error.
-(void)addToFavs:(id)sender event:(id)event {
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if(indexPath!=nil){
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
if ([self.favs count]==0) {
NSLog(#"adding first favorite %#",[self.objects objectAtIndex:indexPath.row]);
[self.favs addObject:[self.objects objectAtIndex:indexPath.row]];
[self.subtitlesFavs addObject:[self.subtitles objectAtIndex:indexPath.row]];
[self.iconsFavs addObject:[self.icons objectAtIndex:indexPath.row]];
}else if (indexPath.section==0){
NSLog(#"removing favorite %#",[self.favs objectAtIndex:indexPath.row]);
[self.favs removeObjectAtIndex:indexPath.row];
[self.subtitlesFavs removeObjectAtIndex:indexPath.row];
[self.iconsFavs removeObjectAtIndex:indexPath.row];
}else if(indexPath.section==1 && [self.favs containsObject:[self.objects objectAtIndex:indexPath.row]]){
NSLog(#"removing favorite %#",[self.objects objectAtIndex:indexPath.row]);
[self.favs removeObject:[self.objects objectAtIndex:indexPath.row]];
[self.subtitlesFavs removeObject:[self.subtitles objectAtIndex:indexPath.row]];
[self.iconsFavs removeObject:[self.icons objectAtIndex:indexPath.row]];
}else{
NSLog(#"adding favorite %#",[self.objects objectAtIndex:indexPath.row]);
[self.favs addObject:[self.objects objectAtIndex:indexPath.row]];
[self.subtitlesFavs addObject:[self.subtitles objectAtIndex:indexPath.row]];
[self.iconsFavs addObject:[self.icons objectAtIndex:indexPath.row]];
}
[tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//cell
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell){
// left image
UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(7, 7, 30, 30)];
[image.layer setCornerRadius:image.frame.size.width/2];
[image setClipsToBounds:YES];
[image.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[image.layer setBorderWidth:0.3f];
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.textColor=[UIColor lightGrayColor];
cell.textLabel.text=self.objects[indexPath.row];
cell.detailTextLabel.text =self.subtitles[indexPath.row];
[cell.contentView addSubview:image];
if(indexPath.section==0){
image.image=[UIImage imageNamed:[self.iconsFavs objectAtIndex:indexPath.row]];
cell.textLabel.text=self.favs[indexPath.row];
cell.detailTextLabel.text =self.subtitlesFavs[indexPath.row];
}else{
image.image=[UIImage imageNamed:[self.icons objectAtIndex:indexPath.row]];
}
//separetor
[self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 45, 0, 0)];
//favorites image button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, 25, 25);
button.frame = frame;
button.showsTouchWhenHighlighted = YES;
[button setImage:[UIImage imageNamed:#"unfavorites.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"favorites.png"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(addToFavs:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
}
return cell;
}
I create a tableview has button, this button has been changed image ass different condition. The first load tableview, these buttons set image correct but after condition change, it set image incorrect. Example : If [Selling = 0] , set image "Market.png", else [Selling =2], set image "MarketSelect.png". When value of Selling array changing, i used [_tableview reloadData] but it still not change image. I debug, i see MarketButton is 0x0000. The first load, MarketButton works correct but second load, it is 0x0000. Please give me advice to solve this problem. I tried 2 days but it can not works. thanks much
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.button.tag = indexPath.row;
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market setFrame:CGRectMake(200, 6, 30, 30)];
market.tag = 4000;
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:market];
UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 30)];
pricelabel.backgroundColor = [UIColor clearColor];
pricelabel.font = [UIFont fontWithName:#"Helvetica" size:16];
pricelabel.font = [UIFont boldSystemFontOfSize:16];
pricelabel.textColor = [UIColor darkGrayColor];
pricelabel.tag = 3000;
//pricelabel.hidden = YES;
pricelabel.textAlignment = NSTextAlignmentRight;
[cell.contentView addSubview: pricelabel];
[pricelabel release];
}
UIButton *marketButton = (UIButton*)[cell.contentView viewWithTag:4000];
[marketButton setTag:indexPath.row];
if([sellingArray count]>0)
{
NSLog(#"sellingArray %#",sellingArray);
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"0"]) // nothing
{
[marketButton setSelected:NO];
[marketButton setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
}
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"2"]) // marketplace
{
[marketButton setSelected:YES];
[marketButton setImage:[UIImage imageNamed:#"MarketplaceSelect.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
}
}
}
UILabel *pricelbl = (UILabel*)[cell.contentView viewWithTag:3000];
pricelbl.text =[NSString stringWithFormat:#"$%#",[priceNewArray objectAtIndex:indexPath.row]];
if ([sellingArray count]>0) {
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"2"]){
pricelbl.hidden = NO;
}
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"0"]){
pricelbl.hidden = YES;
}
}
return cell;
}
marketpressAction :
- (void)marketPressedAction:(id)sender
{
buttonPressed = (UIButton *)sender;
buttontag = buttonPressed.tag;
NSLog(#"Market button click at row %d",buttontag);
}
I added UIButton to each uitableview row but when i nedd reloadData, uibutton is 0x0000. Can you give me any suggestions? Thanks much.
I used this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/*NSString *CellIdentifier = [NSString stringWithFormat:#"%d,%d",indexPath.section,indexPath.row];
// NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{*/
NSString *CellIdentifier = [NSString stringWithFormat:#"%d,%d",indexPath.section,indexPath.row];
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 30)];
pricelabel.backgroundColor = [UIColor clearColor];
pricelabel.font = [UIFont fontWithName:#"Helvetica" size:16];
pricelabel.font = [UIFont boldSystemFontOfSize:16];
pricelabel.textColor = [UIColor darkGrayColor];
pricelabel.tag = 3000;
//pricelabel.hidden = YES;
pricelabel.textAlignment = NSTextAlignmentRight;
[cell.contentView addSubview: pricelabel];
[pricelabel release];
UIButton * market = [[UIButton alloc] init];;
[market setFrame:CGRectMake(200, 6, 30, 30)];
market.tag = 4000;
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
// [market setTag:indexPath.row];
[cell.contentView addSubview:market];
}
if([priceNewArray count]> 0)
{
UILabel *pricelbl = (UILabel*)[cell.contentView viewWithTag:3000];
pricelbl.text =[NSString stringWithFormat:#"$%#",[priceNewArray objectAtIndex:indexPath.row]];
if ([sellingArray count]>0) {
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"2"]){
pricelbl.hidden = NO;
}
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"0"]){
pricelbl.hidden = YES;
}
}
}
UIButton *marketButton = (UIButton*)[cell.contentView viewWithTag:4000];
[marketButton setTag:indexPath.row];
if([sellingArray count]>0)
{
NSLog(#"sellingArray %#",sellingArray);
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"0"]) // nothing
{
[marketButton setSelected:NO];
[marketButton setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
}
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"2"]) // marketplace
{
[marketButton setSelected:YES];
[marketButton setImage:[UIImage imageNamed:#"MarketplaceSelect.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
}
}
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
return cell;
}
I used above code, when reloadData, data of Price label is changed fine but marketbutton can not changed image. I debug after call [_tableview reloadData];, marketButton is 0x0000 .
I think your comparison is not correct. You are trying to compare integer with string.
If i asumed correct then try something like this.
int sellingPrice=0;
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString: [NSString stringWithFormat:#"%i",sellingPrice]]) // nothing
sellingPrice=2;
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:[NSString stringWithFormat:#"%i",sellingPrice]]) // marketplace
use single CellIdentifier for cell when using same designs for cell,it used to reuse tableview cells
NSString *CellIdentifier = #"Cell"
please remove this code [marketButton setTag:indexPath.row]; you not want to change tag for cell,you not set tag for identifier for UIButton, change this two thinks your code will work