开发者

UITableView adding cells dynamically

I have a UITableView in my nib file and would like to dynamically add some content to each of the cells in that TableView. Is there a way to do this? I have an array of text that I would like to display开发者_如何学运维 in the TableView as well as an array of Pictures.


You need to implement the UITableViewDataSource protocol and override the

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

and

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

methods. You will want to return the length of your array for the tableView:numberOfRowsInSection: method. In the tableView:cellForRowAtIndexPath: method you will get create the UITableViewCell (dequeue one if available) and add the UIViews you want to hold your data (i.e. a UIImage, etc.). Access the data to populate your view using the indexPath.row as the index to your array(s). Does that all make sense? It sounds a bit more complicated than it is in practice.

Here is the documentation for the UITableViewDataSource protocol


My ideal is to register as an observer for each cell, then the interested content has changed, then it sends event or necessary data to those cells. By comparing some information, like current indexPath, or some unique identifier of cell, the cell can decide to accept those sent data and change himself, or just pass this sent event and data.

I has implemented above for loading thumbnail image in the background, when the image has been loaded, it notify those cells to update images. And if any source data has been modified, it will notify the those registered cells, then those cells will reload necessary data to update their content.


If you want add cell and data on that cell dynamically from one view controller to other Tableview controler----

Step--1:[[NSNotificationCenter defaultCenter] postNotificationName:@"ReturnTableView" object:(send array object or any object)];

Step--2:go to your Table View controller

Step--3:In YourTableView.h file add this method : - (void)change_view:(NSNotification *)notif;

Step--4:Now Came in YourTableView.m file add the line in viewDidLoad ---[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change_view:) name:@"ReturnTableView" object:nil];

Step--5:Now Add the method in 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];
}

} Step--6:Now Add Finally in

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

    UILabel *label;

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

    return cell;

}

Now Your Data Is Added
Thanks-----

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜