简体   繁体   English

更改特定单元格的UIButton

[英]Change UIButton for Specific cell

Having issues updating uibutton in specific uitableviewcell. 在特定的uitableviewcell中更新uibutton时遇到问题。 When I change the button color it updates every button in uitableview. 当我更改按钮颜色时,它将更新uitableview中的每个按钮。 Attached my code below: 附上我的代码如下:

  PFObject *post = [self.objectArray objectAtIndex:indexPath.section];

    cell.likeBtn.layer.cornerRadius    = 5.0f;

    //To access button methods
    [cell.likeBtn    setTag:indexPath.section];


    NSMutableDictionary *attributes = [NSMutableDictionary
                                       dictionaryWithDictionary:[[TMMemoryCache sharedCache] objectForKey:post.objectId]];


    BOOL likedByCurrentUser = [[attributes objectForKey:@"likedByCurrentUser"] boolValue];

    if (likedByCurrentUser) {

        cell.likeBtn.backgroundColor = [UIColor flatRedColor];
        [cell.likeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [cell.likeBtn setTitle:@"Liked" forState:UIControlStateNormal];
    }else{

        //NSLog(@"Did not like %@", post.objectId);
        cell.likeBtn.backgroundColor = [UIColor flatBlueColor];
        [cell.likeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [cell.likeBtn setTitle:@"Like" forState:UIControlStateNormal];
    }

is their a better way to update uibutton in just one specific cell? 是仅在一个特定单元中更新uibutton的更好方法吗?

There are two ways to do what you want, depending on how the updated information comes in: 有两种方法可以执行所需的操作,具体取决于更新的信息的来源:

  1. When configuring the cell in cellForRowAtIndexPath: . cellForRowAtIndexPath:配置单元格时。
  2. Let the cell manage its own contents. 让单元管理自己的内容。

More often than not, #1 is the way to go. #1通常是必经之路。 When new information comes into your UITableViewController , do 1 of the following: 当新信息进入您的UITableViewController ,请执行以下一项操作:

  1. Call reloadData to force a refresh of the entire table. 调用reloadData以强制刷新整个表。
  2. Figure out which cells need to be updated and call reloadRowsAtIndexPaths:withRowAnimation: passing in the correct index path values. 找出需要更新的单元格,然后调用reloadRowsAtIndexPaths:withRowAnimation:传递正确的索引路径值。

In either of those scenarios, you'll set up the button correctly somewhere in your tableView:cellForRowAtIndexPath: method. 在这两种情况下,您都将在tableView:cellForRowAtIndexPath:方法中的某个位置正确设置按钮。 Generally, I create my UITableViewCell subclasses to accept an object and configure themselves with the data in that object. 通常,我创建UITableViewCell子类来接受一个对象,并使用该对象中的数据进行自身配置。 That way, there's less code in my UITableViewController that deals with cell configuration. 这样, UITableViewController中处理单元配置的代码就更少了。

A typical implementation of tableView:cellForRowAtIndexPath: looks like this: tableView:cellForRowAtIndexPath:典型实现如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"item" forIndexPath:indexPath];
    [cell configureForItem:self.dataSource[indexPath.row]];
    return cell;
}

Each of your UITableViewCell instances will have their own button that will be configured by the data passed by the object in self.dataSource[indexPath.row] . 每个UITableViewCell实例都将具有自己的按钮,该按钮将由对象在self.dataSource[indexPath.row]传递的数据进行配置。 That way, as cells are recycled, the button isn't just passed around, but a new one is recycled and configured each time a new cell is needed. 这样,在回收单元时,按钮不仅会传递,而且每次需要新单元时,都会回收并配置一个新按钮。

At a guess, without seeing more of your code I would say your issue is with your use of indexPath.section instead of indexPath.row 猜测,在没有看到更多代码的情况下,我会说您的问题是使用indexPath.section而不是indexPath.row

If you have multiple buttons per section then they will all be covered by this code. 如果每个部分有多个按钮,则此代码将全部覆盖它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM