reducing size of the section in tableview - iphone

i am very much new to iphone development ,I wanted to reduce the size of a section in the grouped table view i.e reducing the width of the section . how can it be implemented
thanks in advance

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return kHeightForHeaderInSection;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// wrapperView - by default has the width of the table, you can change the height
// with the heightForHeaderInSection method
UIView *aView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
// your real section view: a label, a uiview, whatever
CGRect myFrame; // create your own frame
myFrame.origin = CGPointMake(10, 0);
myFrame.size = CGSizeMake(tableView.bounds.size.width-10,kHeightForHeaderInSection);
UILabel *label = [[[UILabel alloc] initWithFrame:myFrame] autorelease];
label.text = #"myText"
[aView addSubview:label];
return aView;
}

Hi add this UITableviewdelegate method
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return height;
}

its a very simple soln ...
#interface MyController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableview *myTableview;
just right click on "UITableViewDelegate" and "UITableViewDataSource"
n implement all the delegates....u will find all types of adjustment over there....weather u want to change or adjust height or width of section or row ...or how many section or how many rows in a section do u want....
its simple n called automatically.... regards

Related

How to tune this UITableView so that I can get what I want?

I am trying to fit a static UITableView into a UIViewController
The problem I am facing right now as you can see from the image is that
This is a UITableView in a "Plain" mode (instead of "Grouped")
You can see the bottom line (where the table ends) shows correctly. But there is no line at the top (where the table begins).
Is there any way that I can add one line there either programatically or via xcode, other than just drag in a UIView which has a grey background with 1 point for the height?
Thanks
Try to set a 1 pixel wide header
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)]; //It will stretch
view.backgroundColor = [UIColor grayColor];
}

how to increase and resize the tableview cell in iphone?

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}
In this place, i would like to add a button dynamically to increase the height of the cell, so when user clicks upon that button it should be increase the height of cell and then click again to resize the height.
I want to something like :
-(void)IncreaseCell
{
UIButton *DownArrow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[DownArrow addTarget:self
action:#selector(IncreaseCell:)
forControlEvents:UIControlEventTouchDown];
//[DownArrow setTitle:#"Arrow" forState:UIControlStateNormal];
DownArrow.frame = CGRectMake(121.0, 112.0, 72.0, 37.0);
[cell.contentView addSubview:DownArrow];
UIImage *buttonDown = [UIImage imageNamed:#"friendsDownArrowButton.png"];
[DownArrow setBackgroundImage:buttonDown forState:UIControlStateNormal];
[cell.contentView addSubview:DownArrow];
}
You will need to create a NSMutableArray as an instance variable, in which you keep track of all the cells, that you want to have "increased".
#interface YourTableViewController : UITableViewController {
NSMutableDictionary *increasedRows;
}
Remember to alloc/init that variable.
To get your cell increased:
-(void)increseCell: (BOOL)status forIndexPath: (NSIndexPath*)indexPath {
[increasedRows setObject:[NSNumber numberWithBool:status] forKey: indexPath];
[self.tableView beginUpdates]; //Delete if you don't want it animated
[self.tableView endUpdates]; //Delete if you don't want it animated
// [self.tableView reloadData]; //Uncomment if you don't want it animated
}
You change your tableView: heightForRowAtIndexPath: declaration to check this dictionary for your indexPath.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Check if cell is increased, return custom height
if([[increasedRows objectForKey:indexPath] boolValue]) {
return 150;
}
// Cell isn't increased so return regular height
return 50;
}
This method will allow you to do it for every row individually and allows you to animate it.
You need to change your implementation of heightForRowAtIndexPath.
Add some storage, like an array, which holds flags indicating if a row is expanded or not. When a button is tapped, get the index path of the cell that it is on and edit the array contents to set / unset the flag. Reload the table view (or the row for the changed index path).
All you have to do is declare a variable:
#interface yourViewController ()
{
CGFloat cellSize;
}
And in your heightForRowAtIndexPath::
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return cellSize;
}
in your viewDidLoad
-(void)viewDidLoad
{
[super viewDidLoad];
cellSize = 50;
}
and last in your increaseCell:
-(void)IncreseCell {
cellSize += 10;
[self reloadData];
}
what i did in one of my project is i created a custom cell and add subview to it statically
1 st scene non collapsed view
custom cell shows only button on click of which it will show extended view by increasing size of cell..
i.e on click of button you should increse size of cell using function..
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL check=[[booleanArrayofrows objectAtIndex:indexPath.row] boolValue];
if (check)
{
return 180; ..expanded view height
}
return 40; collapse view height
}
}

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

How to implement a TableView where the background scrolls at the same time?

Imagine you have a normal table view where each row is an item on a conveyor belt. You will put the items in each cell of the table view but when you scroll you also want the background image (the conveyor belt) to scroll as well. How can you do this?
You should be able to accomplish this by setting the background color of the table view:
tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
Have the UITableView background transparent, and add a UIScrollView behind it with the UIImageView inside it. Add a listener for when the UITableView scrolls (since it is a subclass of UIScrollView it has all the same delegate methods). Then, when it scrolls, set the scroll position of the UIScrollView behind it to the same programmatically.
You could technically do it without a second UIScrollView behind the UITableView, just with a plain UIIImageView, if you want to reverse the offset values.
I haven't tried this, so I'm not sure what the best approach would be, but one option would be to add your background image as a UIImageView to each of your cells so that every cell has a full-sized copy of your background image. Set clipsToBounds to NO on your cell, and give the bounds of the UIImageView a negative y value equal to the offset from your cell to the top of the table.
You may also want to consider using UIScrollView instead of UITableView.
UITableView is itself a UIScrollView, so you could try just adding your background image as a subview of your UITableView, but I'd be surprised if that worked. I'm guessing the UITableView implementation won't play nice with foreign subviews.
** EDIT **
While I still suspect that UIScrollView may be a more appropriate base class to use here, I decided to try the UIImageView trick I described above. It's fairly simple and doesn't consume excessive memory as long as all your UIImageViews share a single UIImage. Here's my sample code:
// LadderCell.h
#import <UIKit/UIKit.h>
#interface LadderCell : UITableViewCell {
UIImageView *backgroundImageView;
UILabel *titleLabel;
}
#property (nonatomic, retain) UIImageView *backgroundImageView;
#property (nonatomic, retain) UILabel *titleLabel;
- (void)setIndexPath:(NSIndexPath *)indexPath;
- (id)initWithImage:(UIImage *)theImage;
+ (NSString *)reuseIdentifier;
+ (CGFloat)height;
#end
// LadderCell.m
#import "LadderCell.h"
#implementation LadderCell
#synthesize backgroundImageView, titleLabel;
- (void)dealloc {
self.backgroundImageView = nil;
self.titleLabel = nil;
[super dealloc];
}
- (id)initWithImage:(UIImage *)theImage {
if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[LadderCell reuseIdentifier]]) {
self.frame = CGRectMake(0.0, 0.0, 320.0, [LadderCell height]);
self.clipsToBounds = YES;
self.backgroundImageView = [[[UIImageView alloc] initWithImage:theImage] autorelease];
backgroundImageView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
self.titleLabel = [[[UILabel alloc] initWithFrame:self.bounds] autorelease];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
titleLabel.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
[self addSubview:backgroundImageView];
[self addSubview:titleLabel];
}
return self;
}
- (void)setIndexPath:(NSIndexPath *)indexPath {
backgroundImageView.frame = CGRectMake(0.0,
-(CGFloat)indexPath.row * [LadderCell height] + 100.0,
backgroundImageView.frame.size.width,
backgroundImageView.frame.size.height);
}
+ (NSString *)reuseIdentifier {
return #"LadderCell";
}
+ (CGFloat)height {
return 30;
}
#end
// TableBackgroundTestViewController.h
#import
#interface TableBackgroundTestViewController : UITableViewController {
UIImage *backgroundImage;
}
#property (nonatomic, retain) UIImage *backgroundImage;
#end
// TableBackgroundTestViewController.m
#import "TableBackgroundTestViewController.h"
#import "LadderCell.h"
#implementation TableBackgroundTestViewController
#synthesize backgroundImage;
- (void)dealloc {
self.backgroundImage = nil;
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.backgroundImage = [UIImage imageNamed:#"background.png"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
return 1000;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [LadderCell height];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
LadderCell *cell = (LadderCell *)[tableView dequeueReusableCellWithIdentifier:[LadderCell reuseIdentifier]];
if (!cell) {
cell = [[[LadderCell alloc] initWithImage:self.backgroundImage] autorelease];
}
[cell setIndexPath:indexPath];
cell.titleLabel.text = [NSString stringWithFormat:#"Row %d", indexPath.row];
return cell;
}
#end
My suggestion is similar to Ed Marty (what he suggests "without a second UIScrollView"):
Place the UITableView inside a simple UIView. Make the cell backgrounds transparent so background from below the tableview would show
Below the UITableView, place an UIImageView with your desired background. Both the UITableView and UIImageView now sit inside the same enclosing UIView.
Listen to scroll events of UITableView. When detecting a scroll, simply change the background UIImageView position (frame.origin.y) appropriately, so that it would "stick with" the tableview.
You can have the background as one gigantic image, or have a series of them so you do tiling. You can have an array of the images and add to the array from top/bottom when needed, and also remove the images that have "scrolled away" from screen to conserve memory. You will need to calculate the positions for all these background images yourself, but there's nothing complicated in that.

Reusable TableView header views

For performance sake it is usual to reuse UITableView' cells.
Is there a way to do the same thing with TableView header' views?
I am talking about the ones that are returned with delegate's method:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
I tried to do the following which doesn't seem to be working as expected:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static NSString *CellIdentifier = #"Header";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
cell = [self getHeaderContentView: CellIdentifier];
}
return cell;
}
Is there a way to reuse header' views?
The reason Apple built in the ability to reuse tableview cells is because while the tableview may have many rows, only a handful are displayed on screen. Instead of allocating memory for each cell, applications can reuse already existing cells and reconfigure them as necessary.
First off, header views are just UIViews, and while UITableViewCell is a subclass of UIView, they are not intended to be placed as the view of a section header.
Further, since you generally will have far fewer section headers than total rows, there's little reason to build a reusability mechanism and in fact Apple has not implemented one for generic UIViews.
Note that if you are just setting a label to the header, you can use -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section instead.
For something more custom, such as a label with red text (or a button, image, etc), you can do something like this:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)] autorelease];
UILabel *label = [[[UILabel alloc] initWithFrame:headerView.frame] autorelease];
label.textColor = [UIColor redColor];
label.text = [NSString stringWithFormat:#"Section %i", section];
[headerView addSubview:label];
return headerView;
}
You can implement that by creating UITableViewHeaderFooterView class
it is subclass of UIView
You also need to create an individual XIB as it will not be created automatically with UITableViewHeaderFooterView creation.
Register Nib with tableview
[self.tblCart registerNib:[UINib nibWithNibName:#"CartHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:#"CartHeader"];
Now You can Access that in viewForHeaderInSection
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
CartHeaderView *sectionHeader=[tableView dequeueReusableHeaderFooterViewWithIdentifier:#"CartHeader"];
return sectionHeader;
}
Note :
To set background color you will need to create a subview with same frame as section header and set color for that view.
you can follow
Changing the background color on a UITableViewHeaderFooterView loaded from a xib says to use contentView.backgroundColor instead
A simple yet effective solution:
#interface SectionedTableViewController ()
#property (nonatomic, strong) UINib* sectionHeaderNib;
#property (nonatomic, strong) NSMutableArray* sectionHeaders;
#end
#implementation SectionedTableViewController
#synthesize sectionHeaderNib = sectionHeaderNib_;
#synthesize sectionHeaders = sectionHeaders_;
- (void) viewDidUnload
{
self.sectionHeaders = nil;
[super viewDidUnload];
}
- (NSMutableArray*) sectionHeaders
{
if (!sectionHeaders_)
self.sectionHeaders = [NSMutableArray array];
return sectionHeaders_;
}
- (UINib*) sectionHeaderNib
{
if (!sectionHeaderNib_)
self.sectionHeaderNib = [UINib nibWithNibName: NSStringFromClass(YourHeaderView.class) bundle: nil];
return sectionHeaderNib_;
}
- (YourHeaderView*) dequeueHeader
{
return [self.sectionHeaders firstObjectPassingTest: ^(id obj) { return (BOOL) ([obj superview] == nil); }];
}
- (NSString*) sectionHeaderTitleForSection: (NSInteger) section
{
return nil;
}
- (UIView*) tableView: (UITableView*) tableView viewForHeaderInSection: (NSInteger) section
{
YourHeaderView* headerView = [self dequeueHeader];
if (!headerView)
{
headerView = [YourHeaderView instanceFromNib: self.sectionHeaderNib];
[self.sectionHeaders addObject: headerView];
}
return headerView;
}
#end