首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何设置按钮的索引路径?

如何设置按钮的索引路径?
EN

Stack Overflow用户
提问于 2016-06-10 20:29:21
回答 3查看 583关注 0票数 0

使用下面的代码,我试图获得视频点赞计数,我正在获得数组中的计数,我想将其设置为一个按钮标题,所有工作正常。

我有一个按钮动作,有了这个按钮,我就能显示出这个按钮上的点赞了。但我的问题是,使用这段代码,我在表视图中的所有剪辑中都得到了相同的like count,所以我想要获得各自点击like按钮的特定like count,我该怎么做呢?

代码语言:javascript
运行
复制
// 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);
}

这就是我显示的点数按钮

代码语言:javascript
运行
复制
- (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];
  }

cellForRowAtIndexPath的代码如下此代码不包含我的任何按钮代码按钮操作likeButtonTapped是单独的

代码语言:javascript
运行
复制
    - (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;
    }
EN

回答 3

Stack Overflow用户

发布于 2016-06-10 20:41:12

您可能忘记了为每个按钮设置一个tag

您可以通过编程或使用故事板来设置tag

编辑

完整代码:

代码语言:javascript
运行
复制
- (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];
}
票数 0
EN

Stack Overflow用户

发布于 2016-06-10 20:42:31

如果你正在做下面的事情,那么应该没有问题..

代码语言:javascript
运行
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /* - - - - */
    /* - - - - */

    [cell.yourButton setTag:indexPath.row];

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

获取按钮索引的其他替代方法是:

代码语言:javascript
运行
复制
-(IBAction)likeButtonAction:(id)sender
{
    CGPoint buttonPosition  = [sender convertPoint:CGPointZero toView:yourTable];
    NSIndexPath *indexPath  = [yourTable indexPathForRowAtPoint:buttonPosition];
    NSInteger rowIndex      = indexPath.row;

    // Use this index path
}

如果使用tag进行转换,请将sender转换为UIButton对象,如下所示:

代码语言:javascript
运行
复制
-(IBAction)likeButtonAction:(id)sender
{    
    UIButton *button = (UIButton *)sender;
    NSLog(@"%d", button.tag);

    /* - - - - */
    /* - - - - */
}
票数 0
EN

Stack Overflow用户

发布于 2016-06-10 21:43:32

我写了一个简单的示例,就像你写的一样,它是有效的。

这是快照,代码流动,你可以看到按钮文字将在胶带后设置

代码:

代码语言:javascript
运行
复制
- (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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37748333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档