【问题标题】:how to pass the NSMutableArray to UITableView如何将 NSMutableArray 传递给 UITableView
【发布时间】:2014-07-15 02:26:53
【问题描述】:

数据值我有一个文本文件和两个按钮。一个按钮是保存,第二个按钮是显示。 当单击保存按钮数据将存储在核心数据中,然后我单击显示按钮数据将显示在控制台上。一切顺利。 但现在我想当点击显示按钮数据将显示在 UITableView 所以请给我任何想法 提前致谢, 这是我的代码:-

#import "SetViewController.h"
#import "AppDelegate.h"

@interface SetViewController ()
{

    NSMutableArray *array;
}
@end

@implementation SetViewController
@synthesize textField;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)save:(id)sender {
    AppDelegate *appD=(AppDelegate *) [[UIApplication sharedApplication]delegate];
    NSEntityDescription *entit=[NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:appD.managedObjectContext];

    [entit setValue:textField.text forKey:@"fname"];
    NSError *error;
    BOOL isSaved=[appD.managedObjectContext save:&error];
    NSLog(@"succesfully saved flag: %d",isSaved);

}

- (IBAction)show:(id)sender {

    AppDelegate *appDelegat=(AppDelegate*)[[UIApplication sharedApplication]delegate];

    NSEntityDescription *entity=[NSEntityDescription entityForName:@"Team" inManagedObjectContext:appDelegat.managedObjectContext];

    NSFetchRequest *fetchRr = [[NSFetchRequest alloc]init];
    [fetchRr setEntity:entity];


    array=[[appDelegat.managedObjectContext executeFetchRequest:fetchRr error:nil]mutableCopy];
    NSLog(@"array %@",array);


    for (NSManagedObject *obj in array) {
        NSLog(@"Name :%@\n",[obj valueForKey:@"fname"]);

    }

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{

    [textField resignFirstResponder];
    return YES;
}

@end

i am tried like this :-
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"ShowData"])
    {

        ShowViewController *destViewController = segue.destinationViewController;
        destViewController.second = [[array valueForKey:@"fname"] componentsJoinedByString(angry)" , "];
        NSLog(@" got data for array %@",destViewController.second);
    }
}

ShowViewController.h:-

#import <UIKit/UIKit.h>
#import "SetViewController.h"

@interface ShowViewController : UITableViewController<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSString *second;
@end

ShowViewController.m:-

import "ShowViewController.h"

@interface ShowViewController ()
{
    NSMutableArray *acess;
}
@end

@implementation ShowViewController
@synthesize second;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];



    acess =[second componentsSeparatedByString:@", "];


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [acess count];
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    }
    cell.textLabel.text = [acess objectAtIndex:indexPath.row];
    return cell;

}

ShowViewController 是 UITableView。当点击 UItableView 时打开但数据不会显示所以请给我任何想法

【问题讨论】:

  • 句间加点更容易理解
  • 您是否研究过 tableview 编程指南或搜索过 tableview 教程 - 周围有很多。您需要实现表视图委托和数据源协议
  • @Paulw11 感谢重播现在我添加代码请检查然后给我任何关于我的问题的想法
  • 第二个属性是什么类型? ShowViewController 中的 cellForRowAtIndexPath 方法是什么?
  • @Paulw11 请看一下我的代码现在添加 ShowViewController 然后给我任何想法

标签: ios objective-c uitableview core-data


【解决方案1】:

按照以下步骤,同时阅读苹果提供的 UITableview 编程指南:

  1. 获取数组后,只需重新加载您的表格视图。
  2. 使行数 = 数组计数。
  3. 根据需要自定义单元格。

【讨论】:

    【解决方案2】:

    如果您只按照代码中显示的方式声明数组,那么您必须在视图加载时初始化它:

    array=[NSMutablearray array];
    

    之后从 db 中获取值并存储在数组中,然后使用 UITable 视图委托和数据源方法。

    【讨论】:

      【解决方案3】:

      首先在你的 viewDidload() 方法中声明你的数组。

      - (void)viewDidLoad
      {
      yourArray=[[NSArray alloc]initWithObjects:Obj1,obj2,obj3,obj4,obj5, nil];
      }
      

      然后使用 tableview 委托方法。将数组数据设置为表格视图。

      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
      {
      
          // Return the number of sections.
           return 1;
      }
      
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {
      
           // Return the number of rows in the section.
           return [yourArray count];
      }
      
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
      
             // Configure the cell...
      
         if (cell == nil)
          {
             cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
           }
      
             cell.textLabel.text=[yourArray objectAtIndex:indexPath.row];
             return cell;
      }
      

      【讨论】:

        【解决方案4】:

        如果您已经有一个包含字符串的NSArray,则无需遍历整个 componentsJoinedByString/componentsSeparatedByString - 您只需将数组传递给您的第二个视图控制器。

        访问viewDidLoad 中传递的数据将不起作用,因为当您在prepareForSegue 中设置属性时,视图已经加载。

        您的 prepareForSegue 应该是 -

        - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
            if ([segue.identifier isEqualToString:@"ShowData"])
            {
        
                ShowViewController *destViewController = (ShowViewController *)segue.destinationViewController;
                destViewController.fnames = [array valueForKey:@"fname"];
        
            }
        }
        

        ShowViewController 的 .h 文件中声明一个属性以接收帧 -

        @property (strong,nonatomic) NSArray *fnames;
        

        那么你的表数据源方法将是-

        -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
        {
            return [self.fnames 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] ;
            }
            cell.textLabel.text = [self.fnames objectAtIndex:indexPath.row];
            return cell;
        
        }
        

        【讨论】:

        • 我按照你说的运行,但它仍然显示空的 tableView 所以请给我任何想法
        • 您需要使用调试器并单步执行您的代码 - 变量设置为应有的值还是为零?
        • 另外,请确保您的表方法正在被调用 - 您是否已将数据源和委托属性分配给您的视图控制器类?
        • 当 print destViewController.fnames 显示空值
        • 所以,你需要找出原因。在 prepreForSegue 中设置断点。 valueForKey 返回一个有效的数组吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多