How to create check mark in the table view in iPhone?

帅比萌擦擦* 提交于 2019-12-11 02:03:23

问题


I want to create a check mark in the table view. When I tap the particular row, the check mark is visible, so I want to display the check mark when selected at the particular row. Please guide me and give me some links.

Here my code is,

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

                [self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
          }

I want to display the accessory type, when I click the particular state(selected only one row at a time).

Thanks.


回答1:


Make sure you delegate is set properly for this controller and that the table view is connected to the delegate on the controller:

tableview.delegate = self;

After that your code should work. I tried your code in my own app and it worked (minus the self.tableview, since my tableview is not a property of the controller.)




回答2:


So you can do this by showing an image of check mark in your row.

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

    int x = tableView.frame.size.width - 20; //or whatever x coordinate
    int y = [self.tableView cellForRowAtIndexPath:indexPath].frame.size.height - 10;
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, 20, 20)];
    imgView.image = [UIImage imageNamed:@"image.png"];
    [self.tableView cellForRowAtIndexPath:indexPath] addSubview:imgView];
}


来源:https://stackoverflow.com/questions/4369890/how-to-create-check-mark-in-the-table-view-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!