UITableViewdynamic添加单元格

我在我的笔尖文件中有一个UITableView ,并想dynamic地添加一些内容到该TableView中的每个单元格。 有没有办法做到这一点? 我有一个我想在TableView中显示的文本数组以及一个图片数组。

你需要实现UITableViewDataSource协议并覆盖

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

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

方法。 您将希望返回您的数组的长度为tableView:numberOfRowsInSection:方法。 在tableView:cellForRowAtIndexPath:方法中,您将获得创buildUITableViewCell (如果可用,请先出队)并添加要保存数据的UIView (即UIImage等)。 访问数据以使用indexPath.row作为数组的索引来填充视图。 这一切都有道理吗? 这听起来有点复杂。

这里是UITableViewDataSource协议的文档

我的理想是注册为每个单元格的观察者,然后感兴趣的内容已经改变,然后它发送事件或必要的数据到这些单元格。 通过比较一些信息,比如当前的indexPath,或者一些单元的唯一标识符,小区可以决定接受那些发送的数据并改变自己,或者只是传递这个发送的事件和数据。

我已经实现了在后台加载缩略图图像,当图像被加载时,它通知那些细胞更新图像。 如果有任何源数据被修改,它会通知那些注册的单元格,然后这些单元格将重新加载必要的数据来更新它们的内容。

如果你想在单元格上dynamic添加单元格和数据从一个视图控制器到其他Tableview控制器—-

步骤1:[[NSNotificationCenter defaultCenter] postNotificationName:@“ReturnTableView”对象:(发送数组对象或任何对象)];

第二步:去你的桌面视图控制器

第三步:在YourTableView.h文件中添加这个方法: – (void)change_view:(NSNotification *)notif;

步骤4:现在在YourTableView.m文件中添加viewDidLoad中的行— [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change_view 🙂 name:@“ReturnTableView”object:nil];

步骤5:现在在YourTableView.m中添加方法—- – (void)change_view:(NSNotification *)notif {

 if([[notif name] isEqualToString:@"ReturnTableView"]){ Your Object Ref(Obj-1)=[notif object]; [Obj-1 addObjectsFromArray:self.AnotherMutableArray(obj-2)]; self.obj-2=Obj-1; [self.tableView reloadData]; } 

步骤-6:现在添加终于在

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@“CellIdentifierName”];

    UILabel *标签;

    label =(UILabel *)[cell viewWithTag:TagNo(eg:0)]; label.text = [self.messageposts objectAtIndex:indexPath.row]; label =(UILabel *)[cell viewWithTag:TagNo(eg:1)]; label.text = [self.messageposts objectAtIndex:indexPath.row];

    返回小区;

}

现在您的数据被添加
谢谢 – – –