我在TableViewController中创建了不同的Class对象:
#import "IphoneTabelle.h"
#import "ViewController.h"
#import "Dogs.h"
#import "Cats.h"
@interface IphoneTabelle ()
@end
@implementation IphoneTabelle
- (void)viewDidLoad {
[super viewDidLoad];
/
//create dog object for array
Dogs *dogClass = [[WLAN alloc]init];
_createdClassObjectArrayPics = dogClass.picsDogs;
_createdClassObjectArrayText = dogClass.textDogs;
//Cat objects created for array
Cats *catClass = [[ControlCenter alloc]init];
_createdClassObjectArrayPics = catClass.picsCats;
_createdClassObjectArrayText = catClass.textCats;
//create objects if they dont exists
if (!_objects ){
_objects = [[NSMutableArray alloc] init];
}
//put the class-objects array in objects array
[_objects addObject:NSStringFromClass([dogClass class])];
[_objects addObject:NSStringFromClass([catClass class])];
因此,当用户点击Cell时,会使用segue。图片和文本应该从ViewController.h放入一个数组中。这些数组用于在ViewController中显示图片和文本。
segue Code
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
ViewController *sendDataToViewController = segue.destinationViewController;
NSIndexPath *indexPath = nil;
NSArray *picturesArrayTableView = [[NSArray alloc]init];
NSArray *texteArrayTableView = [[NSArray alloc]init];
if([segue.identifier isEqualToString:@"show"]){
picturesArrayTableView = _createdClassObjectArrayPics[indexPath.row];
texteArrayTableView = _createdClassObjectArrayText[indexPath.row];
//picsArray and textsArray are declared in ViewController.H file
sendDataToViewController.picsArray= picturesArrayTableView;
sendDataToViewController.textsArray = texteArrayTableView;
}
所以当用户点击Cell时,我每次都会收到错误: 'NSInvalidArgumentException',原因:' - [__ NSCFConstantString count]:无法识别的选择器发送到实例0x10becc198'
不知何故,图片和文本中的数据不在picturesArrayTableView和texteArrayTableView中。
如何只用一个segue工作?我不想为每个细胞/物体做一个segue。这种方式确实有效,但创造数百个细分市场确实令人讨厌和混乱。
我希望我能以某种方式澄清我的问题。
答案 0 :(得分:0)
按如下方式创建Dog类:
@interface Dog : NSObject
@property (nonatomic, strong) NSMutableArray *pics;
@property (nonatomic, strong) NSMutableArray *texts;
@end
创建对象并将其添加到数组中
Dog *dogObject=[[Dog alloc]init];
dogObject.pics = [[NSMutableArray alloc]init];
dogObject.texts = [[NSMutableArray alloc]init];
//Add some Pics to dog
[dogObject.pics addObject:[UIImage imageNamed:@"dog.png"]];
//or you can add objects from another array
//Add Text for that pic
[dogObject.texts addObject:@"Cute Dog"];
//Note that Pics and text array will have same count
//create objects if they dont exists
if (!arrObject ){
arrObject = [[NSMutableArray alloc] init];
}
[arrObject addObject:dogObject];
点按表格
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"Show" sender:indexPath];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"Show"])
{
//getting indexpath for selected Item
NSIndexPath *indexPath = (NSIndexPath *)sender;
id selectedObject=[arrObject objectAtIndex:indexPath.row];
//getting View Controller
ViewController *sendDataToViewController = segue.destinationViewController;
//pass selected item to destingation View Controller
sendDataToViewController.selectedItem = selectedObject;
}
}
在详细视图控制器中捕获传递的对象。为此,您需要创建Property in Detail ViewController类。
@property (nonatomic, strong) id selectedItem;
现在您可以访问此对象的照片和文字。
if ([selectedItem isKindOfClass:[Dog class]])
{
// we got the dog object
Dog *dogObject =(Dog *)selectedItem;
//access Pics Like follow
UIImage *image = [dogObject.pics objectAtIndex:0];
NSString *text = [dogObject.texts objectAtIndex:0];
}
else [selectedItem isKindOfClass:[Cat class]]
{
//Same above Thing
}
这只是一个例子,你可以按照自己的意愿使用。