【问题标题】:NSMutableArray data not loading in tableView in iPhone?NSMutableArray 数据未加载到 iPhone 的 tableView 中?
【发布时间】:2012-09-25 05:21:09
【问题描述】:

我对从 WCF 服务获得的内容进行了 XML 解析。我使用NSXMLParser 将数据加载到NSMutableArray。现在我希望将数据加载到UIView 的tableview 中。但我可以看到正在加载的空表视图和数据没有。无法理解我哪里出错了。 我的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;
}   

我得到一些空行的空表视图。我该如何解决这个问题?

【问题讨论】:

  • 你分配数组了吗
  • 是的,我在 load() arr=[[NSMutableArray alloc] init];
  • 然后尝试通过设置断点来初始化你的数组,它是否携带值。如果是,请检查它崩溃的位置。
  • 是的,向 numberOfRows 添加断点以检查数组是否是您认为的那样。类似的东西。
  • 是的,它正在初始化并且我正在获取值。但是不能将断点输入 TableView

标签: iphone uitableview xcode4


【解决方案1】:

嘿,您只需在 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];



 }

【讨论】:

  • 但是我想要像 Roy 123 这样的数组的整个数据在一行中,再在另一行中使用 Joy 123,在另一行中使用 Rony 5983。我该怎么做?
  • 检查我编辑的答案,如果有任何疑问,请告诉我,如果有用,不要忘记接受答案并支持... :)
  • 查看其帮助完全接受的答案,但其声誉不佳.. lolz
  • 是的,我已将其标记为答案 donno y it got - 声誉。嘿,如果我尝试添加另一个 UILabel,我有一个小问题,然后我得到错误:程序收到信号 EXC_BAD_ACCCESS" 这是我的另一个 UILabel.Im 的代码获取 NSMutableArray 中的值。UILabel *line=[[UILabel alloc]initWithFrame:CGRectMake(12, 45, 280,30)]; line .font=[UIFont fontWithName:@"arial" size:10 ]; [line setTextAlignment:UITextAlignmentLeft]; [line setText:[d valueForKey:@"EmployeeId"]]; [line addSubview:line ]; //这一行出错 // [line release]; 请帮忙
  • bcz u [line release] in comment so you got bad exc remove //
【解决方案2】:

您忘记设置 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;

}

【讨论】:

  • 是的,这就是我在说的。
  • 但是我想要像 Roy 123 这样的数组的整个数据在一行中,再在另一行中使用 Joy 123,在另一行中使用 Rony 5983。我该怎么做?
【解决方案3】:

只需确保以下几点:

  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;

}

这肯定会解决你的问题。

【讨论】:

    【解决方案4】:

    检查并确认您的代表,这可能是没有调用 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;
    }
    

    【讨论】:

    • 嘿,我希望将整个数据分成三行,每行分开..但我收到错误:程序在这一行收到信号 SIGABRT :cell.textLabel.text= tempstring;
    • 告诉你是否开始获取数据。
    • 不,但我想要的是像 Roy 123 在一排,Joy 123 在另一排,Rony 5983 在另一排。我该怎么做?我不需要单独使用 valueforkey
    【解决方案5】:

    你在 indexPath 访问数组,而这个数组包含字典,所以首先获取数组并将其放入字典并使用 key 值访问它

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

    【讨论】:

      【解决方案6】:

      您的可变数组具有字典类型的元素。因此,在更新表格单元格中的文本时,尝试根据您的 indexPath.row 从数组中获取字典,然后使用 objectForKey: 获取您要使用的任何键。见下面的代码

      你应该尝试使用

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

      您可以使用表格单元格的默认分隔符。

      【讨论】:

      • 实际上我想要整个数组,因为它在一个表中只有三行,即密码后行之间的分隔符
      • 你想要不同的部分,每一秒包含一到两行。对。
      • @arizah :您应该尝试使用 cell.textLabel.text = [[yourMutableArr objectAtIndex:indexPath.row] valueForKey:@"UserName"]; cell.detailTextLabel.text = [[yourMutableArr objectAtIndex:indexPath.row] valueForKey:@"Password"];
      猜你喜欢
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多