简体   繁体   English

uitableview单元格突出显示

[英]uitableview cell highlight

I have a table view in which each cell contains a button. 我有一个表格视图,其中每个单元格都包含一个按钮。 I dont use the didSelectRowAtIndexPath delegate but my custom method for the button action. 我不使用didSelectRowAtIndexPath委托,而是将我的自定义方法用于按钮操作。 I want that on clicking the button inside each cell, that cell should be highlighted or change colour and return to its normal state(colour) when the button in some other cell is clicked(making this cell highlighted).My button action method is: 我希望在单击每个单元格中的按钮时,应单击该单元格中的按钮以使其突出显示或更改颜色并返回其正常状态(颜色)(使此单元格突出显示)。我的按钮操作方法是:

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


}

But this doesn't seem to work. 但这似乎不起作用。 Any help would be appreciated!! 任何帮助,将不胜感激!!

Set the cell selection style in cellForRowAtIndexPath as : 将cellForRowAtIndexPath中的单元格选择样式设置为:

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

        [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
    }

And : 和:

yourButton.tag = indexPath.row;

Add the following line in your method to highlight the cell : 在您的方法中添加以下行以突出显示单元格:

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];

Unselect previous cell : 取消选择上一个单元格:

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

UITableViewCell *previousCell = [yourTable cellForRowAtIndexPath:previousIndexPathHighlighted];

[previousCell setSelected:NO animated:YES];

previousTag = clickButton.tag;

The proper way to go about this is that on your cellForRowAtIndex: method you have a way of knowing if your cell needs to look highlighted or not. 进行此操作的正确方法是在cellForRowAtIndex:方法上,您可以知道单元格是否需要突出显示。 Then, after clicking on a button and configuring whatever you need to do, you should simply do 然后,在单击按钮并配置所需要做的一切之后,您只需执行

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

The other way to go about this, is to access the cell and change it directly. 解决此问题的另一种方法是访问单元并直接更改它。 But not with button.superview.superview , but rather 但不是与button.superview.superview ,而是

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

I think the best way to do it is to make a 我认为最好的方法是制作一个

NSInteger selectedCellIndex;

Set the tags of your buttons in the cellForRowAtIndex method 在cellForRowAtIndex方法中设置按钮的标签

button.tag = indexPath.row

Set the selectedCellIndex when you click the button and reload the table 单击按钮并重新加载表格时,设置selectedCellIndex

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

Then in the cellForRowAtIndex method check if the index is the same as the selectedCellIndex and add a different color to the cell. 然后在cellForRowAtIndex方法中,检查索引是否与selectedCellIndex相同,并为该单元格添加不同的颜色。

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

Remember to do this outside the if( cell == nil ) check, otherwise it wouldn't work. 请记住在if(cell == nil)检查之外执行此操作,否则它将无法正常工作。

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

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