我希望能夠選擇一行或多行,並通過IBAction執行某些操作。假設我有四行,A,B,C和D.如果我選擇A和B,然後按連接到該IBAction的按鈕,則會發生一些情況。使用IBAction(Obj-C)對UITableView中的選定行執行某些操作
我該如何解決這個問題?
代碼:
@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];
}
謝謝!
按「IBAction」是什麼意思? –
@SatishMavani我的意思是按鈕,對不起。連接到該IBAction的按鈕! – Yomo
你想要選擇多行嗎? –