How to send different data to custom cells in the same UITableView? - iphone

This is for a blog app and the issue I'm having is with the layout of the post and its comments.
Ideally the layout would be like this (version 1):
The parent is a UIScrollView, and all other elements you see are inside it. The UITabelView at the bottom receives the comment thread.
This works, but the problem is with the comments UITableView. I thought I could turn off its scrolling and have it "grow" vertically based on the number of comments. But after a few rows, the content gets clipped. That is because the remaining rows are not being rendered within the vertical size of the UIScrollView.
I've read in SO to do away with the UIScrollView - and instead build the whole thing in a UITableView. Create a custom cell on top to hold the UIImageView, another custom cell below for post text, etc.
Version 2
My question is: how do you send a specific portion of a JSON feed to populate custom cells, and then iterate through the comments populating the lower cells?
For example the code below sends/iterates comments to the comments UITableView in version 1.
But how would I send the post image or post text to the custom cells in version 2?
Is there a better approach? Any kind of advice is greatly appreciated.
DetailViewController.h
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"commentCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *post = self.detailItem;
NSArray *commentThread = [post objectForKey:#"comment"];
NSDictionary *comment = [commentThread objectAtIndex:indexPath.row];
NSString *commentText = [comment objectForKey:#"comment_text"];
NSString *commentAuthorName = [comment objectForKey:#"comment_author_name"];
cell.textLabel.text = commentText;
cell.detailTextLabel.text = commentAuthorName;
return cell;
}
JSON
...
{
"post_id": "1463",
"post_title": null,
"post_text": "dffsdjdflkjklk dlfkjsdlfkj",
"comment": [
{
"comment_author_name": "2162",
"comment_text": "yuiyuiiopiop",
},
{
"comment_author_name": "2163",
"comment_text": "tyutyutyutyuu",
},
{
"comment_author_name": "2164",
"comment_text": "sdfsertertr",
},
]
},
...

you can put post text and image into tableViewHeader.
or put the post text and image in tableView section 0
put the comment in section 1
Edit
{
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] init];
//config the image view
UILabel *postContentLabel = [[UILabel alloc] init];//or other UI component
//Config the postContentLabel
UIView *tableHeaderView = [[UIView alloc] init];
[tableHeaderView addSubview:imageView];
[tableHeaderView addSubview:postContentLabel];
//set the tableHeaderView frame
self.tableView.tableHeaderView = tableHeaderView;
}

yeah it is correct either you simply use header view in table view i m putting the example
take a view say headerView and have imageView in it
after response simply put the image in a imageview object
then load the table view like that
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return self.headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 210;
}
then use the cellForRowAtIndexPath as you have used above .

Related

Scroll list of thumbnails

I need to allow my users to scroll through a list of thumbnails in the documents directory on an iPad application. When they tap on one of the thumbnails, I need to know which one was tapped (or have it call a method, which is basically the same thing.)
I know how to put the thumbnails in the documents directory, and how to read them from there. But I’m not sure which widget is best for displaying the content. Is it UITableView? CCScrollLayer? CCMenuAdvanced? Something else?
CCScrollLayer doesn't work as well as UIScrollView.
I use the view to CCdirector for add UIkit.
UIScrollView *scrollView = [UIScrollView alloc] init];
[[[CCDirector sharedDirector] view] addSubview:scrollView];
or add UITableView.
Take all the paths for thumbnails in one array, lets call pathsArray
NSArray *pathsArray = [seld getPathsForAllImages];
Now implement UITableViewDataSource and UITableViewDelegate:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [pathsArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
NSString *pathForImage = [pathsArray objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageWithContentsOfFile:pathForImage];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *pathForImage = [pathsArray objectAtIndex:indexPath.row];
}
I've had all kinds of problems with overlaying a UIScrollView on a cocos2d project. Even with all the suggested hacks, if a single frame takes too long in cocos2d, the UIScrollView just stops working right. I would suspect that would happen with a UITableView as well.
To solve my own problems, I created a custom ScrollNode class. It's really easy to use:
// create the scroll node
ScrollNode *scrollNode = [ScrollNode nodeWithSize:CGSizeMake(size.width, size.height)];
[self addChild:scrollNode];
// Generate some content for the scroll view
// add it directly to scrollView, or to the built in menu (scrollView.menu)
[scrollNode.menu addChild:yourMenuItem];
// Set the content rect to represent the scroll area
CGRect contentRect = [scrollNode calculateScrollViewContentRect];
[scrollNode setScrollViewContentRect:contentRect];

Proper way to add subview to UITableViewCell contentView

In my application i need to display multiple images in a UITableView,so i've already searched a lot in the web for the proper way to load large images to UITableViewCells,to be more clearly i'll divide the procedure that my app execute:
Download images asynchronous;
Save them to NSHomeDirectory();
=> Thins part is working perfectly.
The problem is,how to display the images in the UITableViewCell,i've already tried to add UIImageView's to the cell contentView but the scrolling performance were a bit affected,i've searched on Apple guides and i believe the correct way is adding UIImageView's to the cell and loading the images from NSHomeDirectory(),so:
What's the best way to customize a UITableViewCell and add the UIImageView's(302x302px) to it?
To get the best scroll performance, you must draw the content of the UITableViewCell yourself.
Loren Brichter, the author of the Tweetie app (now the official Twitter app), wrote a very famous blog post on this. Sadly, this blog post has been deleted.
This article may help you, though. It explains the fast scrolling, it has examples and it has a video to a presentation from Loren Brichter.
Basically, what you want to do is to subclass UITableViewCell and override the drawRect:method. To show an image, you would do something like the following:
- (void)drawRect:(CGRect)rect
{
[myImage drawAtPoint:CGPointMake(10, 10)];
}
This way you avoid to layout a lot of subviews.
I have the same question.
I'm doing the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* CustomCellIdentifier = #"CustomCellIdentifier";
CustomCell* cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifier];
}
// add subview to cell
if (cell.customView == NULL) {
cell.customView = [[CustomView alloc] initWithFrame:cell.frame];
[cell.contentView addSubview:cell.customView];
}
// bind cell data
return cell;
}
Firstly, you need to create a custom cells for UITableView & keep going through following points.
set height of each row to 302 pixels as
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 302.0;
}
Use following code to create UIImageView at each cell of table
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"cell"];
const NSInteger IMAGE_VIEW_TAG=1001;
UIImageView *imageView;
if(cell==nil)
{
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"] autorelease];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
imageView =[[[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 302, 302)] autorelease];
imageView.tag=IMAGE_VIEW_TAG;
imageView.contentMode=UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:imageView];
}
imageView=(UIImageView*)[cell.contentView viewWithTag:IMAGE_VIEW_TAG];
[imageView setImage:[UIImage imageNamed:#"image.png"];
return cell;
}
set number of rows you want to display
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
dont forget to add TableView delegate and data source , UITableViewDelegate and UITableViewDataSource

In-app populating of UITableView with Xcode? (iPhone)

I'm brand new to Xcode and Objective-C, and fairly new to programming in general so please correct me if I have a severe misunderstanding.
Here's what I'd like to accomplish:
Create a Table View (empty upon startup)
Have user add and name cells via a Bar Button (pushes to a different view with a name Text Field and "Create" button I'm assuming)
Each created cell pushes to a new Table View with similar add/naming functionality
Then each of those cells push to a Text View
It seems to me that the best way to accomplish the first part is by populating a string array, then assigning those elements to the corresponding cells. That's where I get stuck.
How do I populate a Table View with an array?
Should I create a new secondary Table View for every cell, or just populate the same one differently depending on which parent cell is selected?
And same question as previous for the Text Views at the end of the chain...multiple Text Views or just different text passed each time?
If you got this far, I sincerely thank you and please let me know if I need to clarify anything.
I hope you know how to create a table view.
Now you should create a NSMutableArray to save your strings.
#property (retain) NSMutableArray *stringsArray = _stringsArray;
so your viewDidLoad will look like this.
- (void)viewDidLoad
{
[super viewDidLoad];
UITableView *myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
myTableView.dataSource = self;
myTableView.delegate = self;
[self.view addSubview:myTableView];
[myTableView release];
self.stringsArray = [NSMutableArray array];
}
Now the table view delegates
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.stringsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tmpTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MainCell";
UITableViewCell *cell = [tmpTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [self.stringsArray objectAtIndex:indexPath.row];
return cell;
}
Now when you click on your barbutton to add new user, you must add it to strings array and just call [tableview reloadData];
I didn't understand why you want a text view.
Hope this helps!
Read from here for complete reference of uitableview UITableView Tutorial : Introduction to iPhone TableView

Not refreshing screen in programmatically filled TableView

When I scroll down and up again my text in tableView will disappear.
And my code is:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [screenDefBuild.elementsToTableView count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
ScreenListElements *currentScreenElement = [screenDefBuild.elementsToTableView objectAtIndex:indexPath.row];
cell.textLabel.text = currentScreenElement.objectName;
currentRow++;
return cell;
}
- (void)viewDidLoad
{
[super viewDidLoad];
tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
[tableView setDataSource:self];
[self.view addSubview:tableView];
}
I also want to fill my table view to entire screen. (grey strap on the top).
I don't know what you're doing with this variable
currentRow++;
But whatever you use that for, i'd wager its breaking your code.
the UITableView will call cellForRowAtIndexPath every time a cell is about to appear on screen regardless of whether it has been on screen before or not. When you scroll down and then scroll back up this variable will have increased beyond the bounds of your data, hence you get empty cells.
You need to design this method in such a way that it can create any cell in the table view at any time. You can't rely on the order that the cells will be made and with scrolling you will have to make the same cell over and over again. Only use indexPath to figure out which cell you are currently supposed to be making.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html
Answer for the second part of the question- grey strap. You are adding the table view to current view, so you should use the size property of self.view.frame but not the origin. You want this to be set to 0,0.
Change
tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
to
CGRect viewFrame=self.view.frame;
viewFrame.origin=CGPointZero;
tableView = [[UITableView alloc] initWithFrame:viewFrame];
As for the first part of your question- it's strange, as you seem to do everything properly. One thing i may suggest is to add [tableView reloadData]; in the end of viewDidLoad.

How to set different subview in every section on tableViewCell

guys i want to set different subview in every section, i put my different view into NSMutableArray, so i hope i can access it depend on indexPath.section, this is my code :
for (int i =0; i<promoCount; i++) {
self.textView.text= [NSString stringWithFormat:#"text view :%d",i];
[self.arrayPromotions addObject:self.textView];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellPromotionIdentifier = #"cellPromo";
cell = [tableView dequeueReusableCellWithIdentifier:cellPromotionIdentifier];
if (cell==nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellPromotionIdentifier] autorelease];
}
[cell.contentView addSubview:[self.arrayPromotions objectAtIndex:indexPath.section]];
cell.contentView.layer.masksToBounds = YES;
cell.contentView.layer.cornerRadius = 5.0;
return cell;
}
But it doesn't work, subview just appear in the last section (but content of that subview is right).
iam sure that array have been filled by different view (by doing NSLog).
anybody can help me.????
What are you doing with y? Why divide an int by a float and store it in y?
Looks like you have only one self.textView. You are changing its .text member and adding it to the array multiple times but it is only one object and at the end of the loop you have multiple references to that one object, so all you see is the text it ended up with.