iOS Development-uitableview Single-select multi-select/Check Implementation 1

Source: Internet
Author: User

TableView how to achieve single or multi-choice?

Our direct idea is to modify the style of a cell,

Then modifying the style needs to be done by modifying the corresponding data,

From here we can infer that we need to set a flag bit for the cell corresponding to the data,

When you are selected, modify the flag to refresh that line.

If the single-selection implementation is slightly more complex:

A single selection requires setting a property to hold the last selected row,

The row needs to be modified after the new row has been selected, maintaining


My implementation is as follows:

(1) Create a tableviewcontroller,

To simply use the system's cell style

Set the reuse identifier to Acell

The model class for the cell is person,

person is the corresponding data on the cell and also includes whether the selected flag bit

(2) The left button of the navigation bar is used to submit a single radio result, the right button is used to jump to the check interface



(3) Key code


The person data class, which provides data for the cell

The yes or no of the Ifselected property is related to whether the row cell is marked

  person.h//  app39-table View 8-Radio Check////  Created by Mrbean on 15/7/24.//  Copyright (c) 2015 Yangbin. All rights reserved.//#import <Foundation/Foundation.h> @interface person:nsobject@property (copy,nonatomic) NSString copy,nonatomic data nsstring on the Textlabel data @property (*detail;//cell) Detaillabel @property on *title;//cell ( assign,nonatomic) BOOL ifselected;//is selected @end

Tableviewcontroller

  tableviewcontroller.m//  app39-table View 8-Radio Check////  Created by Mrbean on 15/7/24.//  Copyright (c) 2015 Yangbin. All rights reserved.//#import "TableViewController.h" #import "Person.h" @interface Tableviewcontroller () @property ( strong,nonatomic) Nsmutablearray *marr;//data source @property (strong,nonatomic) Nsindexpath *lastselected;// Last selected amount Index @end@implementation Tableviewcontroller


False data generated at initial time

-(void) viewdidload {    [super viewdidload];    _marr = [[Nsmutablearray alloc]init];    for (int i=0; i<20; i++)//generates a large amount of false data, using the system's cell    {person        *p = [person alloc]init];        P.title = [NSString stringwithformat:@ "%ititle", I];        P.detail = [NSString stringwithformat:@ "%idetail", I];        p.ifselected = no;//is selected, default is NO        [_marr addobject:p];    }}

#pragma mark-Data source

-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {    return 1;}

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {    return _marr.count;}


Configure the display of each cell

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{    UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:@ "Acell" Forindexpath:indexpath];    Person *p = _marr[indexpath.row];    Cell.textLabel.text = P.title;//cell on the title display    Cell.detailTextLabel.text = p.detail;//The following is the key code 1  if ( p.ifselected)//is checked, if yes then mark      cell.accessorytype = uitableviewcellaccessorycheckmark;//tick mark  Else Cell.accessorytype = uitableviewcellaccessorynone;//does not mark    return cell;}


Change data after a row of cells is selected

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{    Nsindexpath *temp = self.lastselected;//the last selected row    if (temp && temp!=indexpath)//If the last selected row exists and is not the one currently selected, leave the previous row unchecked    { Person        *TP = _marr[temp.row];        tp.ifselected = no;//The data for the cell selected before modifying is not selected        [TableView reloadrowsatindexpaths:@[temp] Withrowanimation: uitableviewrowanimationautomatic];//refresh the row    }    self.lastselected = indexpath;//The selected modification to the current line person    *p = _marr[ Indexpath.row];    p.ifselected = yes;//Modify this selected line Choon    [TableView Reloadrowsatindexpaths:@[indexpath] Withrowanimation: uitableviewrowanimationautomatic];//re-refresh this line}

Click Submit to print the selected results

-(Ibaction) Tapsubmit: (Uibarbuttonitem *) sender{person    *select = _marr[_lastselected.row];    Uialertview *alert = [[Uialertview alloc]initwithtitle:@] you selected: "Message:select.title delegate:nil cancelbuttontitle: @ "I know" otherbuttontitles:nil, nil];    [Alert show];}


Receive Memory warning

-(void) didreceivememorywarning {    [super didreceivememorywarning];    Dispose of any resources the can be recreated.}

@end


Operating effect:

Single-selection effect



Related article: Http://blog.csdn.net/yangbingbinga




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS Development-uitableview Single-select multi-select/Check Implementation 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.