所以我有5行,在选择时他们将带行号的整数传递给我的第二个视图控制器。
每个数字都有自己的带有项目的数组,然后应该返回指定行的项目数量。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MasterCell";
MasterCell *cell = (MasterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if (radInt == 0) {
cell.textLabel.text = @"LM";
NSLog(@"My return should have been LM");
}
if (radInt == 1) {
cell.textLabel.text = @"Restauranger";
NSLog(@"My return should have been Rest");
}
if (radInt == 2) {
cell.textLabel.text = [shoppingArray objectAtIndex:indexPath.row];
}
if (radInt == 3) {
cell.textLabel.text = [annatArray objectAtIndex:indexPath.row];
}
//cell.textLabel.text = [listData objectAtIndex:[indexPath row]];
cell.imageView.image = [UIImage imageNamed:@"tab-icon1.png"];
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
这是我的代码,只是为了测试它,它不起作用。 NSLOG工作正常,但数据简单地更新...我做错了什么?每次Nslogs LM,但它在日志中也有1,即所选行(radInt)。
新方法
static NSString *CellIdentifier = @"MasterCell";
MasterCell *cell = (MasterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
cell = [[MasterCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if (radInt == 0) {
cell.textLabel.text = @"LM";
NSLog(@"My return should have been LM");
}
else if (radInt == 1) {
cell.textLabel.text = @"Restauranger";
NSLog(@"My return should have been Rest");
}
else if (radInt == 2) {
cell.textLabel.text = [shoppingArray objectAtIndex:indexPath.row];
}
else if (radInt == 3) {
cell.textLabel.text = [annatArray objectAtIndex:indexPath.row];
}
else {
cell.textLabel.text = @"Noes";
}
//cell.textLabel.text = [listData objectAtIndex:[indexPath row]];
cell.imageView.image = [UIImage imageNamed:@"tab-icon1.png"];
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
之前的观点----
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondViewController *enjoy = [[SecondViewController alloc] init];
if ([[array objectAtIndex:indexPath.row] isEqual:@"Livsmedel"]) {
[enjoy setTitle:[array objectAtIndex:indexPath.row]];
enjoy.radInt = 0;
NSLog(@"0");
}
if ([[array objectAtIndex:indexPath.row] isEqual:@"Restauranger"]) {
[enjoy setTitle:[array objectAtIndex:indexPath.row]];
enjoy.radInt = 1;
NSLog(@"1");
}
if ([[array objectAtIndex:indexPath.row] isEqual:@"Shopping"]) {
[enjoy setTitle:[array objectAtIndex:indexPath.row]];
enjoy.radInt = 2;
}
if ([[array objectAtIndex:indexPath.row] isEqual:@"Annat"]) {
enjoy.radInt = 3;
}
[self performSegueWithIdentifier:@"main" sender:self];
}
答案 0 :(得分:0)
如果将故事板中的segue从单元格连接到下一个控制器,则实际上不需要实现didSelectRowAtIndexPath:你需要实现的是prepareForSegue:您的代码应如下所示:
- (void)prepareForSegue:(UIStoryboardSegue *) segue sender:(id) sender
{
NSInteger row = [self.tableView indexPathForSelectedRow].row;
SecondViewController *enjoy = segue.destinationViewController;
if ([[array objectAtIndex:row] isEqual:@"Livsmedel"]) {
[enjoy setTitle:[array objectAtIndex:row]];
enjoy.radInt = 0;
NSLog(@"0");
}
if ([[array objectAtIndex:row] isEqual:@"Restauranger"]) {
[enjoy setTitle:[array objectAtIndex:row]];
enjoy.radInt = 1;
NSLog(@"1");
}
if ([[array objectAtIndex:row] isEqual:@"Shopping"]) {
[enjoy setTitle:[array objectAtIndex:row]];
enjoy.radInt = 2;
}
if ([[array objectAtIndex:row] isEqual:@"Annat"]) {
enjoy.radInt = 3;
}
}