2016-07-01 5 views
0

16個のオブジェクトを持つ{arrayname == colA}という可変配列があります。これらの要素に個別にアクセスし、tableviewcellのラベルに入れたいと思います。データにアクセスするが、最後に、それは私がやった変更可能な配列からのデータへのアクセス

コードがある...「キャッチされない例外によりにアプリを終了すると、「NSRangeException」のようなエラーを示しています

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
     EasyTamilTeacherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 
      easyTamil = [colA objectAtIndex:indexPath.row]; 
      cell.inputLabel.text = easyTamil; 
     } 
     return cell; 
} 
+1

はENTIEREエラーメッセージを表示します。私は 'forループ'を理解していません。 – Larme

+0

エラーメッセージを完全に表示します。 – user3182143

+0

colAには配列自体が含まれているようですが、十分な要素が含まれていないことを示すエラーメッセージが表示されます。 – Eiko

答えて

2

かなり基本的な:

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

cell.inputLabel.text= [colA objectAtIndex:indexPath.row]; 

return cell; 
} 
+0

再利用されているため、手動でサブビューを 'UITableViewCell'に追加することはお勧めしません。さらに、すでにカスタムの 'UITableViewCell'であれば、labelプロパティを持たなければなりません。 – Larme

+0

@Larme:同意します。答えを編集しました。 – Nitish

+0

これは@Muthukumarが行うこととは全く異なる論理です。テキストを配列に設定しています。 – Eiko

0

チェックデータソースメソッドでは、セクション内の行数を適切に返します。データソース項目の数を返す必要があります。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
     return colA.count; 
} 
0

このコードは、NSMutableArrayNSSStringを追加していることを確認してください細かい

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

     easyTamil = colA[indexPath.row]; 
     cell.inputLabel.text=easyTamil; 
     return cell; 
    } 
+0

配列のようにオブジェクトとして文字列を持っていたようです。 – Nitish

0

を進めています。あなたがオブジェクトで、あなたの文字列を格納しNSMutableArrayにそのオブジェクトを追加する場合
は、その後、

object = [colA objectAtIndex:indexPath.row]; // return you object 

objectから文字列値を取得し、ラベルにそれを渡します。

cell.inputLabel.text = object.myLabel; 

質問にエラーメッセージを追加してください。あなたに正しい答えを与えるのは簡単です。 (データモデルが存在する場合)

0

親切キーチェックし、他のデータモデルをキャスティング

  1. を試してみてください=>値 あなたは
  2. チェック値が有効でアクセスしたいが存在します。

    let indexPath = NSIndexPath(forItem: 0, inSection: 0) 
    let array : NSMutableArray = NSMutableArray(array: [NSDictionary()]) 
    if array.count > indexPath.row{ 
        if let dictionary = array[indexPath.row] as? NSDictionary{ 
         if let name = dictionary.valueForKey("KeyDoesn't Exist") as? String{ 
          print("name == >>", name) 
          //set name 
         } 
        } 
    } 
    
+0

** if array.count> indexPath.row ** は、アウトオブアレイ境界でオブジェクトにアクセスしようとする際に発生するNSRangeExceptionの問題を解決します。 –

0

注意を標識するためにそれを設定する前に値を確保するために聞かせている場合があり使用してみてください: - データソースとデリゲートをバインドすることを忘れないでください、使用されている2つのUITableViewのデリゲートメソッドがあります。 @interface SecondVC - :それは適用されます。この後

のUIViewController < UITableViewDelegate、UITableViewDataSource>

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


return mutArrMessages.count; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath]; 
cell.lblMessage.text = mutArrMessages[indexPath.row] 
return cell; 
} 
関連する問題