1

WCF サービスから取得したコンテンツの XML 解析を行いました。をNSMutableArray使用してデータをロードしました。NSXMLParser今度は、そのデータを のテーブルビューにロードしますUIView。しかし、空のテーブルビューがロードされていることがわかりますが、データはロードされていません。どこが間違っているのか理解できませんでした。私NSMutableArrayはこのようなものです:

(
    {
   UserName="Roy";
   Password="126";
},
    {
    UserName="Joy";
   Password="123";
},
    {
    UserName="Rony";
   Password="5983";
}
-------------
-------------
)

UItableView のコードは次のとおりです。

@interface RootViewController : UIViewController<NSXMLParserDelegate,UITableViewDataSource,UITableViewDelegate>
{

  NSMutableArray *arr;
  UITableView *tableView;
}
- (void)viewDidLoad
{
   UITableView* tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:tableView];    
}
  -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arr count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier=@"Cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil)
    {
        cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
    }
    NSString *tempstring=[arr objectAtIndex:indexPath.row];
    cell.textLabel.text=  tempstring;
    return cell;

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}   

空の行がいくつかある空のテーブルビューを取得しています。どうすればこれを修正できますか?

4

6 に答える 6

0

ねえ、あなたはあなたのcellForRowAtIndexPathデリゲートにこのコードを追加するだけです:-

編集済み

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
static NSString *CellIdentifier=@"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
    cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}

 SMutableDictionary *d = (NSMutableDictionary *) [arr objectAtIndex:indexPath.row]

    UILabel *username = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 280, 30)];
    username.textColor =DARK_VIEW;
    username.font=[UIFont boldSystemFontOfSize:15];      
    [username setTextAlignment:UITextAlignmentLeft];

    [username setText:[d valueForKey:@"UserName"]];
    [cell addSubview:username];
    [username release];

    UILabel *Password = [[UILabel alloc]initWithFrame:CGRectMake(11, 25, 280, 30)];
    Password.textColor =DARK_VIEW;
    Password.font=[UIFont fontWithName:@"arial" size:12];        
    [Password setTextAlignment:UITextAlignmentLeft];

 [Password setText:[d valueForKey:@"Password"]];
  [cell addSubview:Password];
 [Password release];
return cell;

 }



 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
 {
  SMutableDictionary *d = (NSMutableDictionary *) [arr objectAtIndex:indexPath.row]
        NSString *forNExtViewString =[d valueForKey:@"EmployeID"];


  objSectonView =[[cntrSecodviewcontroller alloc]initWithNibName:@"cntrSecodviewcontroller" bundle:nil];
        [self.navigationController pushViewController:objSectonView animated:YES];



 }
于 2012-10-04T08:09:30.620 に答える
0

次のことを確認してください。

  1. 値は配列に保持されます
  2. オブジェクトが配列に追加された後、TableView をリロードします。

    つまり、[tableView reloadData];

そして、メソッドをこれに置き換えるだけです。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier=@"Cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil)
    {
        cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
    }

    //Over here just print the value of your array , to cross check whether the values are retained in the Array 
    NSLog(@"%@",[arr objectAtIndex:indexPath.row]);

    NSString *tempstring= [[arr objectAtIndex:indexPath.row] valueForKey:@"UserName"];
    cell.textLabel.text=  tempstring;
    return cell;

}

これは間違いなくあなたの問題の解決策をもたらすでしょう.

于 2012-10-04T09:27:48.330 に答える
0

これを確認して、デリゲートを確認してください。これが、テーブルビューが呼び出されない理由である可能性があります。

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

    tablView = [[UITableView alloc]initWithFrame:CGRectMake(10, 10, 300, 460)];
    tablView.delegate = self;
    tablView.dataSource = self;
    [self.view addSubview:tablView];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:                        (NSIndexPath*)indexPath

 {

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

    UITableViewCell * cell = [tablView

                              dequeueReusableCellWithIdentifier: SimpleTableIdentifier];

    if(cell == nil) {

        cell = [[[UITableViewCell alloc]

                 initWithStyle:UITableViewCellStyleDefault

                 reuseIdentifier:SimpleTableIdentifier] autorelease];
    }

      UILabel *lbl5 = [[UILabel alloc]initWithFrame:CGRectMake(30, 5, 250, 50);];
      UILabel *lbl6 = [[UILabel alloc]initWithFrame:CGRectMake(30, 60, 250, 50)];

      lbl1.text = [arrayName objectAtIndex:i];
      lbl2.text = [arrayAddress objectAtIndex:i];

      NSString *tempstring=[arr1 objectAtIndex:indexPath.row];
      NSString *tempstring=[arr2 objectAtIndex:indexPath.row];

     [cell addSubview:lbl1];
     [cell addSubview:lbl2];
     // cell.textLabel.text=  tempstring;
      return cell;
}
于 2012-10-04T08:18:53.447 に答える
0

可変配列にディクショナリ型の要素があります。そのため、テーブル セルのテキストを更新する際に、使用するキーに対して、indexPath.row に基づいて配列からディクショナリを取得し、次に objectForKey: を取得しようとします。以下のコードを参照してください

あなたは使用しようとする必要があります

cell.textLabel.text = [[yourMutableArr objectAtIndex:indexPath.row] valueForKey:@"UserName"]; 
cell.detailTextLabel.text = [[yourMutableArr objectAtIndex:indexPath.row] valueForKey:@"Password"];

テーブルセルのデフォルトのセパレーターを使用できます。

于 2012-10-04T09:22:44.993 に答える
0

tableView デリゲートとデータソースを設定するのを忘れました。データソースには、NSDictionaries のコレクションが含まれているようにも見えます。cellForRow では、辞書から値を正しく抽出する必要があります

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITableView* tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];    
}

Nitin Gohelが指摘したように

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier=@"Cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil)
    {
       cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
    }

    cell.textLabel.text = [[arr objectAtIndex:indexPath.row] valueForKey:@"UserName"];
    return cell;

}
于 2012-10-04T08:10:47.380 に答える
0

indexPath で配列にアクセスしており、この配列には辞書が含まれているため、最初に配列を取得して辞書で取得し、key値でアクセスします

cell.textLabel.text = [[arr objectAtIndex:indexPath.row] objectForKey:@"UserName"];
于 2012-10-04T08:16:29.950 に答える