Ios 使用iAction(Obj-C)对UITableView中的选定行执行某些操作
我希望能够选择一行或多行,并使用iAction做一些事情 假设我有四行,A、B、C和D。如果我选择A和B,然后按下连接到该iAction的按钮,就会发生一些事情 我该怎么办 代码:Ios 使用iAction(Obj-C)对UITableView中的选定行执行某些操作,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我希望能够选择一行或多行,并使用iAction做一些事情 假设我有四行,A、B、C和D。如果我选择A和B,然后按下连接到该iAction的按钮,就会发生一些事情 我该怎么办 代码: @implementation ViewController { NSArray *names; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [name
@implementation ViewController {
NSArray *names;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [names count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tableIdentifier = @"tableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
}
cell.textLabel.text = [names objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ((indexPath.row == 0) && (indexPath.row == 2)) {
NSLog(@"John and James selected.");
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Initialize table data
names = [NSArray arrayWithObjects:@"John", @"Michael", @"Hannah", @"James", nil];
}
谢谢
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
} else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
此代码将在所选行上显示勾号
您还必须使用映射选中单元格的数组,在cellForRowAtIndexPath中,您必须验证是否应选中accessoryType
然后在按钮操作中,您可以对所选行执行代码
希望这有帮助 你按“IBAction”是什么意思?@SatishMavani我是说按钮,对不起。一个连接到iAction的按钮!您想要选择多行吗?是的,我想要能够选择多行,然后使用iAction对它们执行操作!查看
UITableView
的文档。它有名为allowsMultipleSelection
和indexPathsForSelectedRows
的方法。我编辑了代码,只使用了这一个!现在检查我已经更新了这是一个可变数组只有你需要声明它是全局的而不是在这个函数中。让我们在你的视图控制器的.h文件中,添加以下内容:@property(nonatomic,retain)NSMutableArray*selectedIndex;