ios - 单击按钮后获取 uitableview 的复选标记值

标签 ios objective-c uitableview

我想在单击按钮时获取 tableview 的多选值。我检查了很多 stackoverflow 问题但无法得到答案。我知道很多人已经实现了这一点,所以我请求有人指导我如何从 tableview 中获取值。我可以从 tableview 中选择多个值。我可以看到复选标记。

下面是我的代码

.h file
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UITableView *specTableView;

@property (nonatomic, retain) NSArray *arForTable;
@property (nonatomic, retain) NSMutableArray *arForIPs;//to capture ids
- (IBAction)btnActionToGetValues:(id)sender;

.m file

- (void)viewDidLoad {
 self.arForTable=[NSArray arrayWithObjects:@"value1",@"value2",@"value3",@"value4", nil];
    self.arForIPs=[NSMutableArray array];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.arForTable count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if([self.arForIPs containsObject:indexPath]){
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    } else {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    cell.textLabel.text=[self.arForTable objectAtIndex:indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if([self.arForIPs containsObject:indexPath]){
        [self.arForIPs removeObject:indexPath];
    } else {
        [self.arForIPs addObject:indexPath];
    }
    [tableView reloadData];

}

//button action for getting values
- (IBAction)btnActionToGetValues:(id)sender {
NSLog(@"ids %@",arForIPs);
}

我得到这个输出

ids ( " {length = 2, path = 0 - 1}", " {length = 2, path = 0 - 2}" )

我的预期输出是

ids ( "Value1", "Value2" )

如果有人已经在这方面工作,请告诉我。

最佳答案

试试这个

-  (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if([self.arForIPs containsObject:[self.arForTable objectAtIndex:indexPath.row]]){
    [self.arForIPs removeObject:[self.arForTable objectAtIndex:indexPath.row]];
} else {
    [self.arForIPs addObject:[self.arForTable objectAtIndex:indexPath.row]];
}
[tableView reloadData];

}

在您的CellforRowatIndexpath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if([self.arForIPs containsObject:[self.arForTable objectAtIndex:indexPath.row]]){
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
    [cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text=[self.arForTable objectAtIndex:indexPath.row];
return cell;
}

您可以获得的输出

//button action for getting values
- (IBAction)btnActionToGetValues:(id)sender {
NSLog(@"ids %@",arForIPs);
}

关于ios - 单击按钮后获取 uitableview 的复选标记值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37088386/

相关文章:

ios - 如何从Bundle获取PNG文件的资源路径?

iOS - UITableViewCell, UIAlertController

ios - 如何使 iOS 7 中 UISearchBar 顶部的区域(在 UITableView 上时)透明?

ios - viewWillAppear 只被调用一次

ios - 在 iOS 中创建持久通知

ios - 将不同版本的应用程序上传到苹果应用商店

ios - 如何删除 TableView ios 的索引路径中的重复项,

objective-c - 是否可以将 NSAttributedString 应用于 CPTTextLayer 的文本

c - Objective-c 中的 Extern C 函数

ios - 服务器拉取后 JSON 或 NSDATA 不完整/被截断