程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-UITableView復制checkmark

ios-UITableView復制checkmark

編輯:編程綜合問答
UITableView復制checkmark

試了很多方法也沒解決,請高手幫忙看看啊。

在我滾動cellForRowAtIndexPath的時候,在tableView添加和刪除選中標記,不知道為什麼。

// Configure the cell...
cell.textLabel.text = [currencyList objectAtIndex:indexPath.row];

// Check to see what currency is currently selected and checkstyle
if ([selectedCurrency isEqualToString:cell.textLabel.text]) {

    // Add a tick to the selected row
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    // Change the text colour
    cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0.5 alpha:1];
    self.checkedIndexPath = indexPath;
} else {
    UITableViewCell *uncheckCell = [self.tableView
                                    cellForRowAtIndexPath:self.checkedIndexPath];
    uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    // Change the font colour back to black
    uncheckCell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
}

最佳回答:


不需要uncheck cellForRowAtIndexPath中的cell,只要重載選中的cell就OK了。

- (void)setCheckedIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *uncheckIndexPath = self.checkedIndexPath;
    _checkedIndexPath = indexPath;
    [self.tableView reloadRowsAtIndexPaths:@[uncheckIndexPath]
                          withRowAnimation:UITableViewRowAnimationLeft];
}

再修改cellForRowAtIndexPath。需要注意,設置UITableViewCellAccessoryNone是給需要返回的cell。

// Configure the cell...
cell.textLabel.text = [currencyList objectAtIndex:indexPath.row];

// Check to see what currency is currently selected and checkstyle
if ([selectedCurrency isEqualToString:cell.textLabel.text]) {

    // Add a tick to the selected row
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    // Change the text colour
    cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0.5 alpha:1];
    self.checkedIndexPath = indexPath;
} else {
    //If cell unchecked
    cell.accessoryType = UITableViewCellAccessoryNone;
    // Change the font colour back to black
    cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved