Table HeaderView iPhone - iphone

I have a UITableView that needs to have something inserted at the top in some cases (not always).
The code (if it were to be a NORMAL) cell would be as follows
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,98,100)];
imgView.image = [UIImage imageNamed:#"image.png"];
cell.imageView.image = imgView.image;
//sets cell text
cell.textLabel.text = #"Number";
but I was told on here that it would be easier to implement a headerView than to change the indexPath each time.
So my question is how would I implement this? Would I use - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section or would I use - (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section? Keep in mind, I have an image to place into the cell (headerView).
Thanks

You would use
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
titleForHeaderInSection: only sets the header as text, where as viewForHeaderInSection: will allow you to add a view, UIImageView, etc.

UIImageView is a subclass of UIView, so it can be returned from
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
This is what you should do since it allows full customization of the header image.
If you instead use
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
then the only thing you have control over is the NSString that you return. You cannot add a background, change the font color, etc. Since you have an image, use the former method.

Related

Grouped Style UITableView doesn't clip subviews

I have a design issue with grouped uitableview, I add uiviews to the leftmost side of each cell extending little over the cell bounds and I want them to be clipped. But neither setting clip subviews in IB nor setting clipsToBounds property in code didn't help.
So far I have set clipsToBounds (for both uitableview itself and individual uitabviewcells) in a number of places but none helped:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[cell setClipsToBounds:TRUE];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:defaultIdentifier];
if (cell == nil)
{
cell = [self reuseTableViewCellWithIdentifier:defaultIdentifier withIndexPath:indexPath];
}
...
[cell setClipsToBounds:TRUE];
...
return cell;
}
- (void)viewDidLoad
{
...
[mainTableView setClipsToBounds:TRUE];
}
Is it possible without changing tableview style to plain?
Update: My previous answer was wrong. Here is an answer that actually shows how to do what you're asking.
https://stackoverflow.com/a/401271/350467
.......
Previous Answer
If you're adding views to the cell's contentView (which you should be, unless you have a good reason), you'll want to set cell.contentView.clipsToBounds = YES;

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

UILabel is not showing values from the UITableview

Sorry guys its me again.
In my project i used UITableview for showing weather.so, I need to show the first row text to UILabel without any button action or watever. I just want cell text to UILabel without any action.
ex:
In the tableview first cell showing text as "Lovely IOS"
I want to show the same text to UILABEL without any action. I took UILabel in the xib.
Thanks in advance
Any sample code please.
you only set action for particular cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 1){
//set action for cell
}
}
In the method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath equate
cell.textLabel.text = [array objectAtIndex:indexPath.row];
LabelName.text= cell.textLabel.text;
You have two options for this either you add add UILable or set the text to he cell.textLabel.text in the following method.
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
You have the data for your cell so what's a big to UIlabel .You just need to set the same text there.
Updated:-
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = cell.textLabel.text;
you will get the text.

update title of selected row in Uitableview

i'm using the following code to get the selected cell in the UiTableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// Mofify the cell here
}
when i change the cell text or color it does not make any change in the actual UitableView
I would suggest that you reload the cell. Use:
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:NO];
You should keep track of which cell was selected, and update the cell in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I don't know why the text isn't updating for you when you change it in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
but even if you get it to work, it will revert if you scroll that cell off the screen.
UITableView is a subclass of UIView and UIView has this method to force it's refresh
- (void)setNeedsDisplay
But if you don't take that change into account in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
you may never see the change.
UITableViewCell has the -titleLabel method which returns a UILabel. Then you might use the setText: and -setColor: methods.
Try doing:
Set the backgroundColor property of the UITableViewCell.
Set the backgroundColor property of the UILabel (may not do what you need).
Set the backgroundView property if the UITableViewCell to a custom class you make by subclassing UIView, where you create a label and color.
Also, you can't just change the cell color, you need to have a data source from which cellForRowAtIndexPath: reads, which sets the cell color there. This way, the color change will persist.

UILabel in header of UITableView in objective c

I am making an iPad app where in I have a grouped TableView with headers for the sections.
i want to add four label in header of tableview
text of four label's are "company", "current", "prev close" ,"change"
or
instead of adding four labels in header, i want to add four text "company", "current", "prev close" ,"change"
What should I do?
Please Help and Suggest.
Thanks.
There is a delegate method of UITableView for custom view of Header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,40)];
UILabel *objLabel = [[UILabel alloc] initWiThFrame:CGRectMake(0,0,320,40)];
objLabel.text = #"Post";
[headerView addSubview:objLabel]; //Similarly You can Add any UI Component on header
[objLabel release];
return [headerView autorelease];
}
If you want to show the Company, Current, prev next as text in section title, then
just append the text in a single string and return this value in the following delegate method.
(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
Hope this helps.
you can implement - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section and you can return what ever view you wanted for each header