2016-10-21 51 views
-1

也許有人可以幫助我。我有不同的類對象。他們將在TableView.m文件中創建,並將數組作爲這些對象的屬性。我如何從這些對象中填充TableView,以便每個單元格都包含對象?當我將對象添加到數組時,我在運行模擬器時出現錯誤。如何從不同的NSObject創建一個TableView?

錯誤: 應用1 [6354:7304461] - [Object1 isEqualToString:]:無法識別的選擇發送到實例0x600000455c00

我認爲的TableView doensn't知道如何創建的單元格。但我沒有足夠的經驗去了解真正發生的事情。

感謝

- (void)viewDidLoad { 
    [super viewDidLoad]; 


//creating first Object of NSObject Class Object1 
    Object1 *object1Class = [[WLAN alloc]init]; 

    object1Class.object1PicsCreated = [[NSMutableArray alloc] init]; 
    object1Class.object1TextsCreated = [[NSMutableArray alloc] init]; 

    [object1Class.object1PicsCreated addObject:object1Class.picsObject1]; 
    [object1Class.object1TextsCreated addObject:object1Class.textsObject1]; 


    //creating second Object of NSObject Class Object2 
    Object1 *object1Class = [[WLAN alloc]init]; 

    object2Class.object2PicsCreated = [[NSMutableArray alloc] init]; 
    object2Class.object2TextsCreated = [[NSMutableArray alloc] init]; 

    [object1Class.object2PicsCreated addObject:object2Class.picsObject2]; 
    [object1Class.object2TextsCreated addObject:object2Class.textsObject2]; 

if (!_objects){ 
      _objects = [[NSMutableArray alloc] init]; 

     } 

[_objects addObject:object1Class] 
[_objects addObject:object2Class] 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return _objects.count; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 



    cell.textLabel.text = [_objects objectAtIndex:indexPath.row]; 


    return cell; 
} 
+1

cell.textLabel.text = [_objects objectAtIndex:indexPath.row]錯誤的初始化,對象的返回類型不是NSString。 –

+0

它是什麼類型,我應該如何初始化它呢? – podoi17

+0

cell.textLabel.text是NSString類型,因此您的對象具有一些標籤文本,您可以使用cell.textLabel.text –

回答

0
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

cell.textLabel.text = [NSString stringWithFormat:@"%@",[_objects objectAtIndex:indexPath.row]]; 
0

的Xcode崩潰,因爲你傳遞的Object1一個實例,其預計NSString的制定者。 UITableView認爲該對象是NSString,因此請撥打NSString的方法(對您而言,它是-isEqualToString:),這是它崩潰的原因。

無論你想展示什麼,在你通過它之前都需要轉換爲NSString

How can I populate the TableView out of these Objects so that every cell contains on object?

這是一個很奇怪的問題。 UITableView的行是UITableViewCell-s。你不能用其他任何東西填充表格。

也許,你應該更詳細地描述你的任務。

相關問題