簡體   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