2
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  ItemsCustomCell *cell; 
  static NSString *CellIdentifier = @"Cell"; 
  cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
  if (cell == nil)   {
     cell = [[ItemsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

  NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ItemsCustomCell" owner:self options:nil];
  cell = [nib objectAtIndex:0];
  cell.itemName.text=titles[indexPath.row];

  [ cell.addButton setBackgroundImage:[UIImage imageNamed:@"add_btn.png"] forState:UIControlStateNormal];    
  [cell.addButton addTarget:self action:@selector(Checktag:event:)forControlEvents:UIControlEventTouchUpInside];
  cell.cellBackgroudImage.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"list_bg.png"] ];


  NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[self.itemsTableView indexPathsForVisibleRows]];

  NSIndexPath *indexPathVisibleRowsLast= [anArrayOfIndexPath lastObject];

  [ cell.addButton setBackgroundImage:[UIImage imageNamed:@"add_btn.png"] forState:UIControlStateNormal];
  int firstVisible=(indexPathVisibleRowsLast.row)-8;   
  CGPoint offset = itemsTableView.contentOffset;
  CGRect bounds = itemsTableView.bounds;
  CGSize size = itemsTableView.contentSize;
  UIEdgeInsets inset = itemsTableView.contentInset;
  float y = offset.y + bounds.size.height - inset.bottom;
  float h = size.height;
  float reload_distance = 10;       

  }        
}  
return cell;

}

This code is used to change image of cell .button when it is clicked

- (void)Checktag:(id)sender event:(id)event 
{   
        UIButton *btnClicked=(UIButton *)sender;
        NSLog(@"%i",btnClicked.tag);
        [ btnClicked setBackgroundImage:[UIImage imageNamed:@"tick_btn.png"]
                                   forState:UIControlStateNormal];
        NSSet *touches = [event allTouches];
        UITouch *touch = [touches anyObject];
        CGPoint currentTouchPosition = [touch locationInView:itemsTableView];
        NSIndexPath *indexPath = [itemsTableView indexPathForRowAtPoint: currentTouchPosition];

        NSString *title=titles[indexPath.row];
        NSString *id1=idAll[indexPath.row];
        [mySelectedItems addObject:title];
        [idSelectedItems addObject:id1];
        NSLog(@"value of indePath.section %d ,indexPath.row %d",indexPath.section,indexPath.row);    
}

when table refresh all the change imaged is set to default

Rich
  • 8,108
  • 5
  • 46
  • 59
  • 5
    What is the question here? – Pancho Apr 25 '14 at 11:47
  • in checktag portion i set the cell button image but when table veiw refresh it lost the images and images set to default . –  Apr 25 '14 at 11:55
  • i donot want tolost data .... –  Apr 25 '14 at 11:56
  • Instead of doing all these action and setting background Image of UIButton in cellForRowAtIndexPath method,you can do all these in CustomCell.and call the delegate or use blocks for the button Action .I have given this concept in one of my answer . http://stackoverflow.com/questions/23103906/how-to-push-a-view-from-custom-uitableviewcell-with-button-action-with-paramete/23119819#23119819 .Try this .You will definately get your answer – Jayaprada Apr 25 '14 at 12:21

2 Answers2

0

Look the last lines of the code

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ItemsCustomCell *cell;
    static NSString *CellIdentifier = @"Cell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)   {
        cell = [[ItemsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ItemsCustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];


        [ cell.addButton setBackgroundImage:[UIImage imageNamed:@"add_btn.png"] forState:UIControlStateNormal];
        [cell.addButton addTarget:self action:@selector(Checktag:event:)forControlEvents:UIControlEventTouchUpInside];
        cell.cellBackgroudImage.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"list_bg.png"] ];


        NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[self.itemsTableView indexPathsForVisibleRows]];

        NSIndexPath *indexPathVisibleRowsLast= [anArrayOfIndexPath lastObject];

        [ cell.addButton setBackgroundImage:[UIImage imageNamed:@"add_btn.png"] forState:UIControlStateNormal];
        int firstVisible=(indexPathVisibleRowsLast.row)-8;
        CGPoint offset = itemsTableView.contentOffset;
        CGRect bounds = itemsTableView.bounds;
        CGSize size = itemsTableView.contentSize;
        UIEdgeInsets inset = itemsTableView.contentInset;
        float y = offset.y + bounds.size.height - inset.bottom;
        float h = size.height;
        float reload_distance = 10;       

    }
    cell.itemName.text=titles[indexPath.row];

    if ([idSelectedItems containsObject:indexPath])
    {
        [ cell.addButton setBackgroundImage:[UIImage imageNamed:@"tick_btn.png"]
                               forState:UIControlStateNormal];
    }

}  
Akhilrajtr
  • 5,170
  • 3
  • 19
  • 30
Pandey_Laxman
  • 3,889
  • 2
  • 21
  • 39
0

On the top of your class

@implementation YourViewController : UIViewController
{
  NSMutableDictionary *selectedRows;
}

Then change this method to look like this

- (void)Checktag:(id)sender event:(id)event 
{   
        UIButton *btnClicked=(UIButton *)sender;
        NSLog(@"%i",btnClicked.tag);
        [ btnClicked setBackgroundImage:[UIImage imageNamed:@"tick_btn.png"]
                                   forState:UIControlStateNormal];
        NSSet *touches = [event allTouches];
        UITouch *touch = [touches anyObject];
        CGPoint currentTouchPosition = [touch locationInView:itemsTableView];
        NSIndexPath *indexPath = [itemsTableView indexPathForRowAtPoint: currentTouchPosition];

        NSString *title=titles[indexPath.row];
        NSString *id1=idAll[indexPath.row];
        [mySelectedItems addObject:title];
        [idSelectedItems addObject:id1];
        NSLog(@"value of indePath.section %d ,indexPath.row %d",indexPath.section,indexPath.row);

// Add this lline of code

    [selectedRows setBool:YES forKey:title];
}

Finally in your cellForRowAtIndexPath

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  ItemsCustomCell *cell; 
  static NSString *CellIdentifier = @"Cell"; 
  cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
  if (cell == nil)   {
     cell = [[ItemsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

  NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ItemsCustomCell" owner:self options:nil];
  cell = [nib objectAtIndex:0];
  cell.itemName.text=titles[indexPath.row];

  [ cell.addButton setBackgroundImage:[selectedRows boolForKey:titles[indexPath.row]] == YES ? [UIImage imageNamed:@"add_btn.png"] : [UIImage imageNamed:@"add_btn.png"] forState:UIControlStateNormal];   
  [cell.addButton addTarget:self action:@selector(Checktag:event:)forControlEvents:UIControlEventTouchUpInside];
  cell.cellBackgroudImage.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"list_bg.png"] ];


  NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[self.itemsTableView indexPathsForVisibleRows]];

  NSIndexPath *indexPathVisibleRowsLast= [anArrayOfIndexPath lastObject];

  [ cell.addButton setBackgroundImage:[UIImage imageNamed:@"add_btn.png"] forState:UIControlStateNormal];
  int firstVisible=(indexPathVisibleRowsLast.row)-8;   
  CGPoint offset = itemsTableView.contentOffset;
  CGRect bounds = itemsTableView.bounds;
  CGSize size = itemsTableView.contentSize;
  UIEdgeInsets inset = itemsTableView.contentInset;
  float y = offset.y + bounds.size.height - inset.bottom;
  float h = size.height;
  float reload_distance = 10;       

  }        
}  
return cell;
}
Pancho
  • 4,099
  • 1
  • 21
  • 32