【问题标题】:Tick mark on pre-populated tableview预先填充的表格视图上的刻度线
【发布时间】:2014-04-28 07:31:11
【问题描述】:

我有一个在 XCODE 中这样创建的 tableview(无代码)。

我试图在按下任一行时在右侧打勾,我使用的是 IOS7。

这引起了一些噩梦。这是我正在实现的代码

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = [UITableViewCell new];


    cell.tintColor = [UIColor blackColor];

    if(cell.selected){
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }else{
        [cell setAccessoryType:UITableViewCellAccessoryNone];

    }


}

【问题讨论】:

  • You appear to be creating a new cell when a row is selected.你觉得这样对吗?
  • 您必须将选定的单元格状态存储在本地,然后在 cellForRowAtIndexPath 中检查它的状态,它之前是否被选中。

标签: ios objective-c uitableview


【解决方案1】:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];


    cell.tintColor = [UIColor blackColor];

    if(cell.selected){
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }else{
        [cell setAccessoryType:UITableViewCellAccessoryNone];

    }


}

【讨论】:

    猜你喜欢
    • 2013-10-04
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    相关资源
    最近更新 更多