【问题标题】:How to set Index Path to a Button?如何将索引路径设置为按钮?
【发布时间】:2016-10-11 10:15:06
【问题描述】:

使用以下代码,我正在尝试获取视频喜欢计数,我正在获取数组中的计数,并且我想将其设置为按钮标题,一切正常。

我有一个按钮操作,通过它,我可以在该按钮上显示喜欢计数。但我的问题是,使用这段代码,我对表格视图中的所有剪辑都获得了相同的点赞数,所以我想为相应的点赞按钮获得特别的点赞数,我该怎么做。

// 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);
}

这就是我显示按钮的方式

- (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 是单独的

    - (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;
    }

【问题讨论】:

  • 你把这个按钮叫做ButtonAction
  • in a tabelView Custom Cell @Anbu.Karthik
  • NSLog [sender tag] 看看它给出了什么......
  • 按钮和表格视图单元格之间的关系是什么?按钮是表格视图单元格的子视图吗?
  • @J.Hunter 不,它不是子视图......我已经在单元格中手动添加了一个按钮......并连接到我的班级并创建了 IBAction

标签: ios objective-c uitableview storyboard tableview


【解决方案1】:

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

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

    [cell.yourButton setTag:indexPath.row];

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

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

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

    // Use this index path
}

如果你使用标签,将sender 转换为UIButton 对象,如下所示:

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

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

【讨论】:

  • 如果我在标题下方使用您的代码其他替代方案则只有前四个剪辑获得正确的计数,即 1,5,0,2,然后该计数重复 1,5,0,2, 1,5,0,2, 1,5,0,2, 1,5,0,2 它像这样重复@NSPratik
  • 但是,如何将按钮添加到表格视图中也很重要。请告诉我你如何向单元格添加按钮..
  • 我从对象库中拖动按钮并放入表视图中,然后从该按钮中通过 Ctrl+单击创建 IBAction 并拖放到 viewControlle.h
  • 如何为每个按钮设置标签?尝试在cellForRowAtIndexPath:中设置标签
  • 我在故事板@NSPratik 中设置标签
【解决方案2】:

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

您可以通过编程或情节提要设置tag


编辑

完整代码:

- (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上设置标签后我应该做什么
  • @guidev 我给按钮一个标签为 115,现在应用程序崩溃说 -[__NSArrayM objectAtIndex:]: index 115 beyond bounds [0 .. 7]'
  • 那是因为 self.devices 只有 7 个元素,而您正试图访问第 115 个元素。
  • 115 是我的按钮标签号...@guidev 那么我将如何获取它,请查看 [getcat] 方法中的获取代码
【解决方案3】:

我写了一个和你一样的简单示例,它可以工作。

这是快照,代码流动,你可以看到点击后按钮文本将被设置

代码:

- (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 查看我的调试器输出
  • @vicky 你能 NSLog 你的self.devices,只打印每个[managedObject valueForKey:@"total_likes"]
  • @J.Hunter 查看 total_likes 输出
猜你喜欢
  • 2022-07-21
  • 1970-01-01
  • 1970-01-01
  • 2012-03-09
  • 2023-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多