UITableView 获取多选,单选的内容

#import <Foundation/Foundation.h>
@interface Person : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *detail;
@property(nonatomic,assign,getter=isSelected)BOOL select;
@end
#import "TableViewController.h"
#import "Person.h"

@interface TableViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *arrayM;
@property(strong,nonatomic)NSMutableArray *selectRows;
@end

@implementation TableViewController


/**
 *  懒加载
 *
 *  @return 返回需要显示的数据
 */
- (NSMutableArray *)arrayM{
    
    if (_arrayM==nil) {
        _arrayM = [NSMutableArray array];
        for (int i=0; i<20; i++) {//填充假数据
            Person *person = [[Person alloc]init];
            person.name= [NSString stringWithFormat:@"%dzc",i];
            person.detail = [NSString stringWithFormat:@"%ddetail",i];
            person.select = NO;
            
            [_arrayM addObject:person];
        }

    }
    
    return _arrayM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _selectRows = [NSMutableArray array]; //
    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    headerView.backgroundColor = [UIColor redColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btn.frame = CGRectMake(headerView.frame.size.width-200, 20, 200, 30);
    [btn setTitle:@"提交复选结果" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(commitAction) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:btn];
    
    [self.view addSubview:headerView];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, headerView.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-headerView.frame.size.height) style:UITableViewStylePlain];
    self.tableView.dataSource =self;
    self.tableView.delegate =self;
    [self.view addSubview:self.tableView];
    
}

#pragma mark --UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSLog(@"%zi",section);
    
    return [self.arrayM count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID =@"cell";
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    Person *person = self.arrayM[indexPath.row];
    cell.textLabel.text = person.name;
    cell.detailTextLabel.text = person.detail;
    if (person.select) {
        cell.accessoryType =UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Person *person = self.arrayM[indexPath.row];
    if (person.isSelected) {
        person.select = NO;
        [_selectRows removeObject:@(indexPath.row)];
    }else{
        person.select = YES;
        [_selectRows addObject:@(indexPath.row)];
    }
    [tableView reloadRowsAtIndexPaths:@ [indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark --commitAction
- (void)commitAction{
    NSMutableString *strs = [[NSMutableString alloc]initWithString:@"你选中的行数"];
    [_selectRows enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        Person *person = self.arrayM[idx];
        NSLog(@"%@",person.name);
        [strs appendFormat:@"%@,",person.name];
    }];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:strs preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:nil];
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取UITableView内容可以通过UITableView的dataSource属性来实现。dataSource属性是一个遵循UITableViewDataSource协议的对象,可以通过该协议方法来获取表格的数据源。 下面是一个示例代码,演示如何获取UITableView内容: ``` // 获取UITableView的dataSource id<UITableViewDataSource> dataSource = tableView.dataSource; // 获取UITableView的section数 NSInteger numberOfSections = [dataSource numberOfSectionsInTableView:tableView]; // 遍历每个section,获取每个section中的row数以及每个row的内容 for (NSInteger section = 0; section < numberOfSections; section++) { // 获取每个section中的row数 NSInteger numberOfRows = [dataSource tableView:tableView numberOfRowsInSection:section]; // 遍历每个row,获取每个row的内容 for (NSInteger row = 0; row < numberOfRows; row++) { // 获取每个row的indexPath NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section]; // 获取每个row的内容 UITableViewCell *cell = [dataSource tableView:tableView cellForRowAtIndexPath:indexPath]; NSString *cellText = cell.textLabel.text; NSLog(@"Section %ld, Row %ld: %@", section, row, cellText); } } ``` 在上述示例代码中,我们首先获取UITableView的dataSource,然后遍历每个section和row,获取每个row的内容。在获取每个row的内容时,我们使用了UITableViewDataSource协议中的tableView:cellForRowAtIndexPath:方法来获取每个row对应的UITableViewCell对象,然后从UITableViewCell对象中获取文本内容

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值