1

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.

2
  • 1
    How do you like to create the tableViewCells. Do you have some code? If yes show it, we can help better with code. Commented Apr 28, 2011 at 21:12
  • I don't have any code. I just put a UITableView in the xib file and would like to dynamically add some content to that UITableView. Commented Apr 29, 2011 at 6:01

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

7 Comments

In the example: developer.apple.com/library/ios/#samplecode/TableViewSuite/… What exactly is the @"MyIdentifier" referring to?
It is essentially like a tag. A string that identifies a type of cell if it has been previously created. The next line in the sample tries to get a cell that has been previously created with that identifier, that is no longer being used (i.e. it has scrolled off-screen). If it can, it reuses the cell, if not, it creates a new cell and gives it a reuse identifier. The reuse identifier is not unique on a per-cell basis, but a per-type-of-cell basis if that makes sense. Let me know if that's at all unclear and I'll try and clarify.
The method is called in the view controller for the UITableView. The method is called anytime the UITableView determines that it needs to display a cell, by calling it's UITableViewDataSource delegate. It passes in the indexPath for the cell it needs to display. The indexPath has the row and the section of the cell, so you can figure out what data to grab using those values.
Also what if there is already some cells in the TableView, how would I get rid of them and refresh the table with new cells?
|
0

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.

Comments

0

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-----

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.