我有兩個數組和一個包含數字和國家的字典,如ViewDidLoad方法所示。 當用戶點擊特定單元格時,該國家將顯示在控制檯中。 我該怎麼做。 在此先感謝。如何通過選擇tableview單元格來獲取字典值?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
arrayofNumbers = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
arrayofCountries = [[NSArray alloc]initWithObjects:@"India", @"America",@"Pakistan",@"Germany",@"Australia",nil];
dictOfCountries = [[NSDictionary alloc] initWithObjects:arrayofNumbers forKeys:arrayofCountries];
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:selectedIndexPath];
NSLog(@"%@", cell.textLabel.text);
}
請修復您的代碼格式化(縮進和擺脫所有多餘的空行)。 – rmaddy