繁体   English   中英

uitableview单元格突出显示

[英]uitableview cell highlight

我有一个表格视图,其中每个单元格都包含一个按钮。 我不使用didSelectRowAtIndexPath委托,而是将我的自定义方法用于按钮操作。 我希望在单击每个单元格中的按钮时,应单击该单元格中的按钮以使其突出显示或更改颜色并返回其正常状态(颜色)(使此单元格突出显示)。我的按钮操作方法是:

- (void)SelectButtonTapped:(UIButton *)button
{


    self.tabBarController.tabBar.userInteractionEnabled = YES;

    AppDelegate *proDel = [[UIApplication sharedApplication]delegate];

    UITableViewCell *cell = (UITableViewCell *)button.superview.superview;

    //MyCustomCell *cell = (MyCustomCell *)button.superview.superview;

     NSIndexPath *indexPath = [homeTable indexPathForCell:cell];


     //cell.backgroundColor = [UIColor lightGrayColor];


    //[homeTable setNeedsDisplayInRect:[homeTable rectForRowAtIndexPath:indexPath]];


    DetailViewController *objDetail=[[DetailViewController alloc] init];

    Home *tempSearchObj=(Home *)[profileArray objectAtIndex:indexPath.row];
    objDetail.firstName=tempSearchObj.userName;


    objDetail.userImageUrl=tempSearchObj.imageUrl;

    objDetail.passedProfileID = tempSearchObj.profileID;

    plsSelectLabel.text = [NSString stringWithFormat:@"%@",objDetail.firstName];

    proDel.globalName = plsSelectLabel.text;

    proDel.globalProfileId = objDetail.passedProfileID;


}

但这似乎不起作用。 任何帮助,将不胜感激!!

将cellForRowAtIndexPath中的单元格选择样式设置为:

if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
    }

和:

yourButton.tag = indexPath.row;

在您的方法中添加以下行以突出显示单元格:

UIButton *clickButton = (UIButton *)sender;

int addButtonIndex = clickButton.tag;

NSIndexPath *indexPathHighlight = [NSIndexPath indexPathForRow:addButtonIndex inSection:0];

UITableViewCell *newCell = [yourTable cellForRowAtIndexPath:indexPathHighlight];

[newCell setSelected:YES animated:YES];

取消选择上一个单元格:

NSIndexPath *previousIndexPathHighlighted = [NSIndexPath indexPathForRow:previousTag inSection:0];

UITableViewCell *previousCell = [yourTable cellForRowAtIndexPath:previousIndexPathHighlighted];

[previousCell setSelected:NO animated:YES];

previousTag = clickButton.tag;

进行此操作的正确方法是在cellForRowAtIndex:方法上,您可以知道单元格是否需要突出显示。 然后,在单击按钮并配置所需要做的一切之后,您只需执行

NSArray *indexes = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:1]];
[self.tableView reloadRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationNone];

解决此问题的另一种方法是访问单元并直接更改它。 但不是与button.superview.superview ,而是

UITableViewCell *cell = [self.tableView cellForRowAtIndex:[NSIndexPath indexPathForRow:0 inSection:1]];

我认为最好的方法是制作一个

NSInteger selectedCellIndex;

在cellForRowAtIndex方法中设置按钮的标签

button.tag = indexPath.row

单击按钮并重新加载表格时,设置selectedCellIndex

selectedCellIndex = button.tag;
[self.tableView reloadData];

然后在cellForRowAtIndex方法中,检查索引是否与selectedCellIndex相同,并为该单元格添加不同的颜色。

if( indexPath.row == selectedCellIndex ){
    //set different colors
}

请记住在if(cell == nil)检查之外执行此操作,否则它将无法正常工作。

暂无
暂无

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

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