0

With the following code, I am trying to get the video likes count, I am getting the count in array and I want to set that to a button title all is working good.

I have a button action and with that, I am able to show the likes count on that button. But my problem is that, with this code, I am getting the same like count to all clips in table view, so I want to get particular like count for respective like button hit how would i do that.

// here is how i am getting like count   

- (void)getcat
{
     NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"ClipTable"];
     NSEntityDescription *entity = [NSEntityDescription entityForName:@"ClipTable" inManagedObjectContext:self.managedObjectContext];

     fetchRequest.resultType = NSDictionaryResultType;
     fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"total_likes"]];
     fetchRequest.returnsDistinctResults = YES;

     NSArray *dictionaries = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
     NSLog (@"total_likes: %@",dictionaries);

     [NSString stringWithFormat:@"%@",[dictionaries valueForKey:@"total_likes"]];

     self.devices =[[NSMutableArray alloc]init];
     self.devices=[dictionaries mutableCopy];
     NSLog(@"cat1  is%@",self.devices);
}

And this is how i am displaying like count on button

- (IBAction)likeButtonAction:(id)sender
 {
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
     NSManagedObject *managedObject = [self.devices objectAtIndex:indexPath.row];
     NSString *likes = [NSString stringWithFormat:@"%@",[managedObject valueForKey:@"total_likes"]];
     [sender setTitle:likes forState:UIControlStateNormal];
  }

code for cellForRowAtIndexPath is below this code does not contain my any button code the button action likeButtonTapped is separate

    - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
// Fetch Record
NSManagedObject *record = [self.fetchedResultsController objectAtIndexPath:indexPath];

     //this button is different one 

 UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(208,96, 100, 30)];
[btn addTarget:self action:@selector(yourButtonClicked:) forControlEvents:UIControlEventTouchUpInside];     btn.tag = indexPath.row;
[btn setImage:[UIImage imageNamed:@"btn-details.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:btn];

NSLog(@"sender.tag cell is%ld ",(long)btn.tag);

   }

  see my debugger output
  indexpath is0
  2016-06-10 19:20:31.435 freejournlaist[4159:134867] indexpath is4
  2016-06-10 19:20:31.436 freejournlaist[4159:134867] sender.tag cell is4 
  2016-06-10 19:20:33.351 freejournlaist[4159:134867] indexpath is5
  2016-06-10 19:20:33.352 freejournlaist[4159:134867] sender.tag cell is5 
  2016-06-10 19:20:34.140 freejournlaist[4159:134867] indexpath is6
  2016-06-10 19:20:34.141 freejournlaist[4159:134867] sender.tag cell is6 
 2016-06-10 19:20:35.506 freejournlaist[4159:134867] indexpath is7
 2016-06-10 19:20:35.507 freejournlaist[4159:134867] sender.tag cell is7  
 2016-06-10 19:20:35.888 freejournlaist[4159:134867] indexpath is8
 2016-06-10 19:20:35.889 freejournlaist[4159:134867] sender.tag cell is8 
 2016-06-10 19:20:36.139 freejournlaist[4159:134867] indexpath is9
 2016-06-10 19:20:36.140 freejournlaist[4159:134867] sender.tag cell is9 
 2016-06-10 19:20:36.475 freejournlaist[4159:134867] indexpath is10
 2016-06-10 19:20:36.476 freejournlaist[4159:134867] sender.tag cell is10 
 2016-06-10 19:20:38.201 freejournlaist[4159:134867] indexpath is6
 2016-06-10 19:20:38.202 freejournlaist[4159:134867] sender.tag cell is6 
 2016-06-10 19:20:38.701 freejournlaist[4159:134867] indexpath is5
 2016-06-10 19:20:38.702 freejournlaist[4159:134867] sender.tag cell is5 
 2016-06-10 19:20:39.267 freejournlaist[4159:134867] indexpath is4
 2016-06-10 19:20:39.268 freejournlaist[4159:134867] sender.tag cell is4 
 2016-06-10 19:20:40.684 freejournlaist[4159:134867] indexpath is3
 2016-06-10 19:20:40.685 freejournlaist[4159:134867] sender.tag cell is3 
 2016-06-10 19:20:40.837 freejournlaist[4159:134867] indexpath is2
 2016-06-10 19:20:40.839 freejournlaist[4159:134867] sender.tag cell is2 
 2016-06-10 19:20:41.152 freejournlaist[4159:134867] indexpath is1
 2016-06-10 19:20:41.153 freejournlaist[4159:134867] sender.tag cell is1 
 2016-06-10 19:20:41.952 freejournlaist[4159:134867] indexpath is0
 2016-06-10 19:20:41.954 freejournlaist[4159:134867] sender.tag cell is0 
 2016-06-10 19:21:02.915 freejournlaist[4159:134867] indexpath is3
 2016-06-10 19:21:02.916 freejournlaist[4159:134867] sender.tag cell is3 

 2016-06-10 19:55:38.055 freejournlaist[4326:144151] total_likes: (
            {
            "total_likes" = 1;
            },
            {
            "total_likes" = 5;
            },
            {
    "total_likes" = 2;
    },
    {
    "total_likes" = 0;
     },
    {
    "total_likes" = 4;
     },
    {
    "total_likes" = 3;
    },
    {
    "total_likes" = 6;
     },
    {
    "total_likes" = 115;
     }
     )
   2016-06-10 19:55:38.055 freejournlaist[4326:144151] cat1  is(
    {
    "total_likes" = 1;
    },
    {
    "total_likes" = 5;
    },
     {
    "total_likes" = 2;
   },
    {
    "total_likes" = 0;
   },
     {
    "total_likes" = 4;
   },
    {
    "total_likes" = 3;
   },
    {
    "total_likes" = 6;
    },
    {
    "total_likes" = 115;
    }
vicky
  • 253
  • 2
  • 14
  • 1
    where u called this button likeButtonAction – Anbu.Karthik Jun 10 '16 at 12:33
  • 1
    in a tabelView Custom Cell @Anbu.Karthik – vicky Jun 10 '16 at 12:34
  • NSLog the `[sender tag]` and see what it gives.. – NSPratik Jun 10 '16 at 12:36
  • what's kind of relationship between your button and table view cell? Is button a subview of table view cell? – J.Hunter Jun 10 '16 at 12:38
  • @J.Hunter no its not subview....i have added a button in cell manually...and connected to my class and created IBAction – vicky Jun 10 '16 at 12:41
  • @NSPratik it gives me cell no. – vicky Jun 10 '16 at 12:42
  • can you show your cellForRowAtIndexPath method – Anbu.Karthik Jun 10 '16 at 12:42
  • Put log on selection of cell printing the cell index path, check whether it gives you proper index or not.. – NSPratik Jun 10 '16 at 12:45
  • post the code you create the button, pls. and, what's logic and flow when user tap the button? Must user select one cell before tap button? – J.Hunter Jun 10 '16 at 12:45
  • no there is custom table view and in every table view cell the same button shows which i added manually i didn't create button programatically in button action i am writing code..@ J.hunter – vicky Jun 10 '16 at 13:00
  • @NSPratik it is giving me error app crashes saying index 9 beyond bounds [0.....7] – vicky Jun 10 '16 at 13:03
  • @vicky [Check this answer](http://stackoverflow.com/questions/11936126/how-to-pass-uitableview-indexpath-to-uibutton-selector-by-parameters-in-ios/11936294#11936294) – TheTiger Jun 10 '16 at 13:03
  • in tablview's cellForRowAtIndexPath, you set button.tag = indexPath.row – J.Hunter Jun 10 '16 at 13:13
  • @ The Tiger in this answer the button is programatically created using CGRect make but in my case i have added button manually because if we create button using CGrectMake it remains on same position in all screens i don't know how to deal with that – vicky Jun 10 '16 at 13:14
  • @ J.Hunter y i know that, but we can do that thing if we create button in the cell using UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(208,96, 100, 30)]; but problem with this is that it remains on same position in every device i don't know how to deal with that – vicky Jun 10 '16 at 13:17
  • you can do the same thing if you create the button in storyboard. in method cellForRowAtIndexPath, you get the cell via `tableView.dequeueReusableCellWithIdentifier("reuseid", forIndexPath: indexPath)`, the button is in the array - `cell.containView.subviews`, you can enumerate subviews and get the button, you can `button.tag = indexPath.row` now – J.Hunter Jun 10 '16 at 13:37
  • i have updated my code added cellForRow code see the code @NSPratik – vicky Jun 10 '16 at 13:38
  • I post answeer, a simple code – J.Hunter Jun 10 '16 at 13:41
  • @J.Hunter see my cellForRowAtIndexPath method – vicky Jun 10 '16 at 13:42
  • I'm puzzled now. In the end you create button with code or in storyboard. I post code under the premise the button is created in storyboard – J.Hunter Jun 10 '16 at 13:49
  • @J.Hunter don't get puzzled that is different button, i have added a comment above that also – vicky Jun 10 '16 at 13:52
  • If you add button in storyboard cell, you don't need to `alloc` it inside `cellForRow`. Find any demo on "How to add cell inside storyboard".. – NSPratik Jun 10 '16 at 13:56
  • but i did not alloc any button in cellForRow that button is different @NSPratik – vicky Jun 10 '16 at 13:59
  • @J.Hunter see my debugger output – vicky Jun 10 '16 at 14:06
  • You printing output of a button, you `alloc` in `CellForRow` – NSPratik Jun 10 '16 at 14:07
  • @ NSPratik no it is of the button on which i am liking on....and the button which i alloc in the CellForRow has different function it is for moving to another view – vicky Jun 10 '16 at 14:10

3 Answers3

0

You probably forgot to set a tag for every button.

You can set a tag either programatically or with Storyboard


Edit

Full Code:

- (void)getcat{

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"ClipTable"];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ClipTable" inManagedObjectContext:self.managedObjectContext];

    fetchRequest.resultType = NSDictionaryResultType;
    fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"total_likes"]];
    fetchRequest.returnsDistinctResults = YES;

    NSArray *dictionaries = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    NSLog(@"total_likes: %@",dictionaries);

    self.devices = [dictionaries mutableCopy];
    NSLog(@"cat1  is%@", self.devices);
}

- (IBAction)likeButtonAction:(id)sender {

    NSManagedObject *managedObject = [self.devices objectAtIndex:sender.tag];

    NSString *likes = [NSString stringWithFormat:@"%@", [managedObject valueForKey:@"total_likes"]];

    [sender setTitle:likes forState:UIControlStateNormal];
}
guidev
  • 2,695
  • 2
  • 23
  • 44
  • what should i do after setting tag on storyboard @guidev – vicky Jun 10 '16 at 12:43
  • @ guidev i have give button a tag as 115 and now the app crashes saying -[__NSArrayM objectAtIndex:]: index 115 beyond bounds [0 .. 7]' – vicky Jun 10 '16 at 13:10
  • that's because self.devices has only 7 elements and you're trying to access the 115th. – guidev Jun 10 '16 at 13:38
  • 115 is my button tag number...@guidev so how would i fetch that please see that fetch code in [getcat] method – vicky Jun 10 '16 at 13:40
0

If you are doing following thing, then there should be no problem..

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

    [cell.yourButton setTag:indexPath.row];

    /* - - - - */
    /* - - - - */
}

Other alternative to get button index is:

-(IBAction)likeButtonAction:(id)sender
{
    CGPoint buttonPosition  = [sender convertPoint:CGPointZero toView:yourTable];
    NSIndexPath *indexPath  = [yourTable indexPathForRowAtPoint:buttonPosition];
    NSInteger rowIndex      = indexPath.row;

    // Use this index path
}

If you do it with tag, convert sender to UIButton object as following:

-(IBAction)likeButtonAction:(id)sender
{    
    UIButton *button = (UIButton *)sender;
    NSLog(@"%d", button.tag);

    /* - - - - */
    /* - - - - */
}
NSPratik
  • 4,714
  • 7
  • 51
  • 81
  • if i use your code below the heading Other alternative then only first four clips get the right count which is 1,5,0,2 and then that count repeats like 1,5,0,2, 1,5,0,2, 1,5,0,2, 1,5,0,2 it repeats like this @NSPratik – vicky Jun 10 '16 at 12:52
  • But, how you add button into the table view also matters. Please show me how you add button to cells.. – NSPratik Jun 10 '16 at 12:53
  • i drag button from object library and drop in table view and then from that button i create IBAction by Ctrl+Click and drag and drop in viewControlle.h – vicky Jun 10 '16 at 12:57
  • How you set tag for each button ? Try to set tag in `cellForRowAtIndexPath:` – NSPratik Jun 10 '16 at 12:59
  • i set tag in story board @NSPratik – vicky Jun 10 '16 at 13:11
  • Try to set in `cellForRowAtIndexPath:` – NSPratik Jun 10 '16 at 13:15
  • i know how to set it in cell UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(208,96, 100, 30)]; using this we can create a button in cell and set an index path for it but problem with this button is that it remains at same position in all the devices with different size i don't know how to deal with CGRectMake – vicky Jun 10 '16 at 13:20
  • You don't need to alloc it in cell `cellForRowAtIndexPath:`. What you need to do is to just set tag in this method only. Cell will be added in storyboard itself.. – NSPratik Jun 10 '16 at 13:29
0

I write a simple sample just like yours, it works.

this is snap shot, codes flowing, you can see button text will be set after taping enter image description here

codes:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.managedObjectContext = ((AppDelegate *)[UIApplication sharedApplication].delegate).managedObjectContext;
    [self getcat];
}

- (void)getcat {
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"ClipTable"];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ClipTable" inManagedObjectContext:self.managedObjectContext];

    fetchRequest.resultType = NSDictionaryResultType;
    fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"total_likes"]];
    fetchRequest.returnsDistinctResults = YES;

    NSArray *dictionaries = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    NSLog (@"total_likes: %@",dictionaries);

    [NSString stringWithFormat:@"%@",[dictionaries valueForKey:@"total_likes"]];

    self.devices =[[NSMutableArray alloc]init];
    self.devices=[dictionaries mutableCopy];
    NSLog(@"cat1  is%@",self.devices);
}

- (IBAction)likeButtonAction:(UIButton *)sender {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
    NSManagedObject *managedObject = [self.devices objectAtIndex:indexPath.row];
    NSString *likes = [NSString stringWithFormat:@"%@",[managedObject valueForKey:@"total_likes"]];
    [sender setTitle:likes forState:UIControlStateNormal];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.devices.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseid" forIndexPath:indexPath];

    // Configure the cell...
    NSArray *subviews = cell.contentView.subviews;
    for (UIView *subview in subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            ((UIButton *)subview).tag = indexPath.row;
        } else if ([subview isKindOfClass:[UILabel class]]) {
            NSManagedObject *managedObject = [self.devices objectAtIndex:indexPath.row];
            NSString *likes = [NSString stringWithFormat:@"%@",[managedObject valueForKey:@"total_likes"]];
            ((UILabel *)subview).text = likes;
        }
    }

    return cell;
}
J.Hunter
  • 576
  • 4
  • 14