【问题标题】:Reload UITableView To Contain Newly inserted Data重新加载 UITableView 以包含新插入的数据
【发布时间】:2015-07-14 12:10:10
【问题描述】:

我有一个 SecondViewController 作为滑动面板,它有一个 UITableView 并使用从 ViewController.Running 的“getnotes”函数返回的 NSArray 数据填充它,它第一次获取所有数据,但是当插入新数据时 UITableView 不会更新虽然 NSArray "n" 包含新数据。

这里是 SecondViewController.h-

     #import <UIKit/UIKit.h>
     @interface SecondViewController:UIViewController<UITableViewDelegate,UITableViewDataSource>
     {
       NSArray *n;

      }
     @property (strong, nonatomic) IBOutlet UITableView *tableview;
     -(void)gets;
     @end

这里是 SecondViewController.m-

#import "SecondViewController.h"
#import "ViewController.h"


@interface SecondViewController ()


@end

@implementation SecondViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self gets];
}

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

#pragma Table View Methods

-(void)gets{

ViewController *dba=[[ViewController alloc]init];
n = [NSArray arrayWithArray:[dba getnotes]];
 
}

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

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

if(n==nil)return 0;

return n.count;

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

     static NSString *cellID=@"cellID";
     UITableViewCell *cell=[tableView  dequeueReusableCellWithIdentifier:cellID];
     if(cell==nil){
           cell=[[UITableViewCell  alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
                  }

    NSDictionary * res =[n objectAtIndex:indexPath.row];
    

    cell.textLabel.text=[res objectForKey:@"NoteText"];
    cell.detailTextLabel.text=[NSString stringWithFormat:@"%@" ,[res   objectForKey:@"NoteDate"]];
    return cell;
   }


  @end 

问题出在哪里?

更新:好的..我不知道它是对还是错,但我调用了gets函数,然后在viewWillAppear方法中重新加载了tableview并且它起作用了:)

【问题讨论】:

  • 您如何从ViewController 显示SecondViewController
  • 为此生成通知
  • secondviewcontroller 是一个半滑动面板......就像手机中的 facebook 一样。 @Akhhilrajtr

标签: ios objective-c uitableview


【解决方案1】:

简单,在数组中插入新数据后只需调用[tableview reloadData]

【讨论】:

  • -(void)gets{ ViewController *dba=[[ViewController alloc]init]; n = [NSArray arrayWithArray:[dba getnotes]]; [self.tableview reloadData]; NSLog(@" 我得到的数组:%@",n); } ...它不起作用
  • 日志打印什么?它有你需要展示的对象吗?
  • yap 日志用新数据打印数组
  • 请检查您的数据源或代理是否已连接,如果没有则,self.tableView.datasource = selfself.tableView.delegate = self
【解决方案2】:

如果您根本没有获得任何数据,我认为您需要设置表视图的 dataSourceDelegate。您可能已经在 IB 中这样做了,但我们在代码中看不到。

也没那么重要,但你的 tableView 插座可能是(弱)。

【讨论】:

    【解决方案3】:

    您必须像这样设置 tableview 数据源和委托。这样做的好地方是在 viewDidLoad 方法中:

    @implementation SecondViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        tabelview.dataSource = self
        tableview.delegate = self
    
        [self gets];
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多