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

您忘记设置 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 回答
0

检查并确认您的代表,这可能是没有调用 tableview 的原因。

- (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

只需确保以下事项:

  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 回答