-(void)addMyButton
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethod) forControlEvents:UIControlEventTouchUpInside];
button.tag = 1;
[button setTitle:#"Show More" forState:UIControlStateNormal];
button.frame = CGRectMake(110.0, 430.0, 100.0, 40.0);
[self.view addSubview:button];
}
-(void)viewDidLoad
{
[self addMyButton]; // Call add button method on view load
n=1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(n==1){
n++;
return 10;
}
else if(n==2){
n++;
return 20;
}
else
return 30;
}
-(void) aMethod
{
if(n>=3)
{
[button setTitle:#"Return" forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
//[button setBackgroundColor:[UIColor brownColor]];
}
else{
// [button setTitle:#"Show More" forState:UIControlStateNormal];
// [button setBackgroundColor:[UIColor clearColor]];
}
NSLog(#"n==>> %d",n);
[tbl reloadData];
}
An array having thirty elements when i click ok return button then it should be shows twenty elements on TableView after next clicked i should shows ten elements and so on..
Please try the edited code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(n==1)
{
n++;
return 20; //edited from return 10 to return 20-- to return 20 elements for the first time
}
else if (n==2)
{
return 30; //Return 30 when click on view more
}
}
-(void) aMethod
{
if(n==2)
{
[button setTitle:#"Return" forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
//[button setBackgroundColor:[UIColor brownColor]];
}
else
{
n=1;
[button setTitle:#"Show More" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor clearColor]];
}
NSLog(#"n==>> %d",n);
[tbl reloadData];
}
try my code and let me know if i miss anything according to your requirement
try like this....
#import "YourTableViewControllerClass.h"
#interface YourTableViewControllerClass ()
{
NSMutableArray * tableArray;
}
#end
#implementation TableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
tableArray = [[NSMutableArray alloc]initWithArray:#[#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10"]];
}
-(IBAction)addMoreCells // button action to add more cells
{
[tableArray addObjectsFromArray:#[#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20"]];
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return tableArray.count ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
cell.textLabel.text = [tableArray objectAtIndex:indexPath.row];
return cell;
}
#end
It looks like you're almost there.
You need tableView:cellForRowAtIndexPath: from the UITableViewDataSource protocol. This method effectively links the correct index in your array to a UITableViewCell.
e.g. something like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"];
}
cell.textLabel.text = [self.myArray objectAtIndex:indexPath.row];
return cell;
}
Try this,
-(void)addMyButton
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethod)forControlEvents:UIControlEventTouchUpInside];
button.tag = 1;
[button setTitle:#"Show More" forState:UIControlStateNormal];
button.frame = CGRectMake(110.0, 430.0, 100.0, 40.0);
[self.view addSubview:button];
}
-(void)viewDidLoad
{
[super viewDidLoad];
[self addMyButton]; // Call add button method on view load
n=2;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if( [your_array count] <= (n*10)){
n=1;
return [your_array count];
}
else
{
return n*10;
}
return 0;
}
-(void)aMethod
{
n++;
if([your_array count]<=n*10){
[button setTitle:#"Return" forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
}
else{
[button setTitle:#"Show More" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor clearColor]];
}
[tbl reloadData];
}
Also add the code to set the data in table view cell (from your array) inside this method,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
You can check the functionality by using the dummy code given below,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"%ld",indexPath.row+1];
return cell;
}
Related
At the very first time when collectionview load it show tag value perfect but when i reload uicollection then the value change and some value are zero why i can't understand please help me.
-(void)CheckUncheckFunctionality:(id)sender event:(id)event
{
UIButton *btn = (UIButton *)sender;
NSInteger subproductindex = btn.tag;
NSLog(#"Sub:%ld",(long)subproductindex);
if ([[arr objectAtIndex:subproductindex] isEqualToString:#"1"])
{
[btn setBackgroundImage:[UIImage imageNamed:#"unchacked.png"] forState:UIControlStateNormal];
[arr replaceObjectAtIndex:subproductindex withObject:#"0"];
}
else
{
[btn setBackgroundImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
[arr replaceObjectAtIndex:subproductindex withObject:#"1"];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CGCell *cell = [_CGCol dequeueReusableCellWithReuseIdentifier:#"CELL" forIndexPath:indexPath];
BOOL isExist;
isExist = [[arr objectAtIndex:indexPath.row] isEqualToString:#"1"];
cell.btn.tag = indexPath.row;
[cell.btn addTarget:self action:#selector(CheckUncheckFunctionality:event:) forControlEvents:UIControlEventTouchUpInside];
if (isExist) {
[cell.btn setBackgroundImage:[UIImage imageNamed:#"checked.png"]forState:UIControlStateNormal];
}
else{
[cell.btn setBackgroundImage:[UIImage imageNamed:#"unchacked.png"]
forState:UIControlStateNormal];
}
return cell;
}
- (void) searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {
searching = YES;
if([searchText length] > 0) {
[arrgroupMember removeAllObjects];
searching = YES;
[self searchCollectionView];
}
else{
searching = NO;
arrgroupMember = [DatabaseAccess GetDataForMember];
}
[_CGCol reloadData];
}
on searching when collectionview gets reload d tag is set to 0.How could i preserve that
My table view uses a custom class to create cells
MyCustomCell.h
#import <UIKit/UIKit.h>
#interface MyCustomCell : UITableViewCell
#end
MyCustomCell.m
#import "MyCustomCell.h"
#import <QuartzCore/QuartzCore.h>
#implementation MyCustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
self.selectionStyle = UITableViewCellSelectionStyleBlue;
// Configure the view for the selected state
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.imageView.frame = CGRectMake(5,4,80,80);
self.imageView.layer.masksToBounds = YES;
self.imageView.layer.cornerRadius = 5.0f;
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
float imgWidth = self.imageView.image.size.width;
if(imgWidth > 0)
{
self.textLabel.frame = CGRectMake(110,2,170,25);
self.detailTextLabel.frame = CGRectMake(110,18,200,25);
self.detailTextLabel.font = [UIFont boldSystemFontOfSize:12];
self.detailTextLabel.alpha = 0.0;
self.textLabel.font = [UIFont boldSystemFontOfSize:12];
self.textLabel.textColor = [UIColor colorWithRed:.2314 green:.3490 blue:.5961 alpha:1.0];
}
}
#end
and is use it like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
if ([UIScreen mainScreen].scale == 2.f && screenHeight == 568.0f)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setBackgroundImage:[UIImage imageNamed:#"select2x.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(SelectButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
btn.frame = CGRectMake(110, 25, 63, 32);
//btn.tag = indexPath.row;
viewProfile = [UIButton buttonWithType:UIButtonTypeRoundedRect];
viewProfile.frame = CGRectMake(190, 25, 93, 32);
//[viewProfile setTitle:#"View Profile" forState:UIControlStateNormal];
[viewProfile setBackgroundImage:[UIImage imageNamed:#"viewProfile#2x.png"] forState:UIControlStateNormal];
[viewProfile addTarget:self action:#selector(viewProfileTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:viewProfile];
[cell.contentView addSubview:btn];
}
else
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setBackgroundImage:[UIImage imageNamed:#"select.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(SelectButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
btn.frame = CGRectMake(110, 25, 63, 32);
//btn.tag = indexPath.row;
viewProfile = [UIButton buttonWithType:UIButtonTypeRoundedRect];
viewProfile.frame = CGRectMake(190, 25, 93, 32);
//[viewProfile setTitle:#"View Profile" forState:UIControlStateNormal];
[viewProfile setBackgroundImage:[UIImage imageNamed:#"viewProfile.png"] forState:UIControlStateNormal];
[viewProfile addTarget:self action:#selector(viewProfileTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:viewProfile];
[cell.contentView addSubview:btn];
}
return cell;
}
Now each cell contains two buttons which perform an action(I do not make action on cell tap but on button tap) as created like above
Now what i want is when a user taps a button(say select button) in a particular cell, that particular cell only should become highlighted or shaded or coloured and when user taps a button in another cell, that cell should become highlighted making the previous cell unhighlighted. I have tried storing previous and next indexpath's in 2 different variables but not able to work on them to achieve what i want.
- (void)SelectButtonTapped:(UIButton *)button
{
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
NSIndexPath *indexPath = [homeTable indexPathForCell:cell];
// UITableViewCell *cell1 = [homeTable cellForRowAtIndexPath:indexPath];
//currentSelection = indexPath.row;
//MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath];
[[homeTable delegate]tableView:homeTable didSelectRowAtIndexPath:indexPath];
//MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath];
/*if(currentSelection == 0)
{
[cell1 setSelected:YES animated:YES];
currentSelection++;
}
else
{
[cell1 setSelected:YES animated:YES];
}*/
// NSIndexPath *indexPath1 = [homeTable indexPathForCell:cell];
NSLog(#"current===>%d",currentSelection);
//[cell1 setSelected:NO animated:YES];
//if(currentSelection == indexPath)
//UIButton *clickButton = (UIButton *)sender;
// NSIndexPath *indexPathHighlight = [NSIndexPath indexPathForRow:button.tag inSection:0];
//button.tag = indexPathHighlight.row;
//UITableViewCell *newCell = [homeTable cellForRowAtIndexPath:indexPathHighlight];
//if(newCell.selected == NO)
//{
//[newCell setSelected:YES animated:YES];
// [newCell setBackgroundColor:[UIColor lightGrayColor]];
//}
//else
// {
//[newCell setSelected:NO animated:YES];
//[newCell setBackgroundColor:[UIColor colorWithRed:232.0f/255.0f green:237.0f/255.0f blue:240.0f/255.0f alpha:1.0f]];
//}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath];
cell1.selectionStyle= UITableViewCellSelectionStyleBlue;
}
You can look at my messy button action method. I have tried many options but with no success. Please help on this.
Uncomment the line :
btn.tag = indexPath.row+1000;
Also change the cell selection style as
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
when create it.
In SelectButtonTapped method :
- (void)SelectButtonTapped:(UIButton *)button
{
int buttonIndex = button.tag;
for (int row = 0; row<numberOfRows; row++)
{
NSIndexPath indexPathHighlight = [NSIndexPath indexPathForRow:row inSection:0];
UITableViewCell *newCell = [yourTable cellForRowAtIndexPath:indexPathHighlight];
if (row == buttonIndex - 1000)
{
[newCell setSelected:YES animated:YES];
}
else
{
[newCell setSelected:NO animated:YES];
}
}
}
Lets think that your table has only one section and tag of your button be the row number of the corresponding cell.
In the button action
int rownumber = button.tag;
//print the rownumber and see if you are getting the correct value
UITableViewCell *cell = [tablename cellForRowAtIndexPath:[NSIndexPath
indexPathForRow:rownumber inSection:0]];
[cell setHighlighted:YES];
Try this -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath];
cell1.selectionStyle= UITableViewCellSelectionStyleBlue;
}
My recent frustration is a UIButton subview in each UITableViewCell of my UITableView which I want to setHidden: according to a specific clause for each indexPath. My code is pretty much the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[self initCell:cell forIndexPath:indexPath];
}
[self updateCell:cell forIndexPath:indexPath];
return cell;
}
and the init and update methods are the following:
- (void)initCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
...
UIButton *btnMy = [UIButton buttonWithType:UIButtonTypeCustom];
btnMy.tag = kButtonMyTag;
[btnMy setFrame:CGRectMake(170, 45, 100, 30)];
[btnMy setBackgroundImage:[UIImage imageNamed:#"btn_image"] forState:UIControlStateNormal];
btnMy.adjustsImageWhenHighlighted = YES;
[btnMy setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btnMy.titleLabel.font = [UIFont fontWithName:#"MyFont" size:14];
[btnMy addTarget:self action:#selector(btnMyPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btnMy];
UIImageView *imgViewAccessory = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"table_accessory"]];
cell.accessoryView = imgViewAccessory;
[imgViewAccessory release];
}
- (void)updateCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
UIButton *btnMy = (UIButton *)[cell viewWithTag:kButtonMyTag];
MyObject *object = (MyObject *)[self.dataSource objectAtIndex:indexPath.row];
if(object.meetsCondition)
{
btnMy.hidden = NO;
}
else
{
btnMy.hidden = YES;
}
...
}
The frustrating result is that when scrolling the button shows and hides randomly and not as expected according the if clause in the updateCell method.
Any help would be much appreciated. Thanks in advance!
You should make custom cell and depending upon the condition show and hide the button
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *nib;
static NSString *cellIdentifier= #"cell";
UITableViewCell *theCell = [self.tblView dequeueReusableCellWithIdentifier:cellIdentifier];
if([theCell.contentView subviews]){
for(UIView *view in [theCell.contentView subviews]){
[view removeFromSuperview];
}
}
if(theCell== nil)
{
nib = [[NSBundle mainBundle] loadNibNamed:#"Your custom cell name" owner:self options:nil];
theCell = [nib objectAtIndex:0];
theCell.selectionStyle = UITableViewCellSelectionStyleNone;
}
UIButton *btn=(UIButton*)[theCell.contentView viewWithTag:101];
if(yourcondition)
//hide button
else
//show button
}
This will do
Use this code in your CellForRowAtIndexPath Also.
MyObject *object = (MyObject *)[self.dataSource objectAtIndex:indexPath.row];
if(object.meetsCondition) {
btnMy.hidden = NO;
}
else {
btnMy.hidden = YES;
}
I', have a UITableView with UISwitch in some cells.
I want to capture events, but it crashes when I try to add the indexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Create cell with all data
UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchview;
[switchview addTarget:self action:#selector(updateSwitchAtIndexPath) forControlEvents:UIControlEventTouchUpInside];
}
- (void)updateSwitchAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UISwitch *switchView = (UISwitch *)cell.accessoryView;
if ([switchView isOn]) {
NSLog(#"ON");
} else {
NSLog(#"OFF");
}
}
It crashes, I think because I'm not adding the indexPath parameter, but I can't get how to set it.
-[ParkingData updateSwitchAtIndexPath]: unrecognized selector sent to instance 0x7b88eb0
Thanks!
UPDATE
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Create cell with all data
UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchview;
[switchview addTarget:self action:#selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)updateSwitchAtIndexPath:(UISwitch *)switchview {
if ([switchView isOn]) {
NSLog(#"ON");
} else {
NSLog(#"OFF");
}
}
[switchview addTarget:self action:#selector(updateSwitchAtIndexPath) forControlEvents:UIControlEventTouchUpInside];
should be
[switchview addTarget:self action:#selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];
you are missing just a colon. because your method has parameter.
- (void)updateSwitchAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchview;
[switchview addTarget:self action:#selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)updateSwitchAtIndexPath:(id)sender
{
// see the line BELOW
NSIndexPath *indexPath = [[(UITableView*)[sender superview] superview]indexPathForCell: (UITableViewCell*)[sender superview]];
if ([sender isOn])
NSLog(#"ON");
else
NSLog(#"OFF");
}
In the above code sender means UISwitch and if u want indexPath of cell then the marked line helps to you.
I am creating an application and I have a customized header of tableview. But sometimes the header gets duplicated. It takes the place of a row in tableview. How do I solve this strange problem? BTW, my tableviewstyle is plain, Header height is 44.0 and the footer height is 0.0. Here is an image how it displays. The header "comments" is duplicated just below the header "Messages" while it should be the row.
Here is the complete implementation of this view
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
TitleForSectionView=[[NSArray arrayWithObjects:#"Dates To Remember",#"Messages",#"Comments",#"Wishlist",#"Reminders",#"Bookmarks",nil] retain];
self.MySectionIndexArray=[[NSMutableArray alloc] init];
[self.MySectionIndexArray addObject:#"UP"];
[self.MySectionIndexArray addObject:#"DOWN"];
[self.MySectionIndexArray addObject:#"DOWN"];
[self.MySectionIndexArray addObject:#"DOWN"];
[self.MySectionIndexArray addObject:#"DOWN"];
[self.MySectionIndexArray addObject:#"DOWN"];
self.IconsForSectionsView=[[NSMutableArray alloc] init];
[self.IconsForSectionsView addObject:[UIImage imageNamed:#"IconCalender.png"]];
[self.IconsForSectionsView addObject:[UIImage imageNamed:#"IconMessage.png"]];
[self.IconsForSectionsView addObject:[UIImage imageNamed:#"IconComments.png"]];
[self.IconsForSectionsView addObject:[UIImage imageNamed:#"IconWishList.png"]];
[self.IconsForSectionsView addObject:[UIImage imageNamed:#"IconReminder.png"]];
[self.IconsForSectionsView addObject:[UIImage imageNamed:#"IconBookMark.png"]];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 6;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([[self.MySectionIndexArray objectAtIndex:section] isEqualToString:#"UP"]) {
return 4;
} else {
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *MyDashBoardCell=nil;
static NSString *AddNewDateCellIdentifier=#"AddNewDateCell";
static NSString *CellIdentifier = #"DashBoardCell";
DashBoardCustomCellObject = (DashBoardCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (DashBoardCustomCellObject == nil) {
[[[[NSBundle mainBundle] loadNibNamed:#"DashBoardCustomCell" owner:self options:nil] retain]autorelease];
}
[DashBoardCustomCellObject SetDashBoardCellData:#"Mar 9" EventText:#"My Birthday"];
AddNewDateCellObject = (AddNewDateCell *)[tableView dequeueReusableCellWithIdentifier:AddNewDateCellIdentifier];
if (AddNewDateCellObject == nil) {
[[[[NSBundle mainBundle] loadNibNamed:#"AddNewDateCell" owner:self options:nil] retain]autorelease];
}
if(indexPath.section==0 && indexPath.row==0)
{
MyDashBoardCell=AddNewDateCellObject;
}
else
{
MyDashBoardCell=DashBoardCustomCellObject;
}
return MyDashBoardCell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// create the parent view that will hold header Label and button and icon image
self.customView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.bounds.size.width,44)] autorelease];
self.customView.backgroundColor=[UIColor whiteColor];
// create image object
UIImageView *icon= [[[UIImageView alloc] initWithImage:[self.IconsForSectionsView objectAtIndex:section]] autorelease];
icon.contentMode=UIViewContentModeCenter;
icon.frame = CGRectMake(0,0,40,40);
// create the label objects
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.font = [UIFont boldSystemFontOfSize:13];
headerLabel.frame = CGRectMake(53,11,172,21);
headerLabel.text = [TitleForSectionView objectAtIndex:section];
headerLabel.textColor = [UIColor blackColor];
headerLabel.highlightedTextColor=[UIColor whiteColor];
// create the button objects
ButtonDrop = [[[UIButton alloc] initWithFrame:CGRectMake(278, 9, 25, 25)] autorelease];
ButtonDrop.tag=section;
if ([[self.MySectionIndexArray objectAtIndex:section] isEqualToString:#"UP"])
{
[ButtonDrop setBackgroundImage:[UIImage imageNamed:#"Up.png"] forState:UIControlStateNormal];
self.customView.backgroundColor=[UIColor blueColor];
headerLabel.highlighted=YES;
}
else
{
[ButtonDrop setBackgroundImage:[UIImage imageNamed:#"Down.png"] forState:UIControlStateNormal];
}
[ButtonDrop addTarget:self action:#selector(checkAction:) forControlEvents:UIControlEventTouchUpInside];
[self.customView addSubview:icon];
[self.customView addSubview:headerLabel];
[self.customView addSubview:ButtonDrop];
return self.customView;
}
- (void)checkAction:(id)sender
{
UIButton *Button = (UIButton*)sender;
NSLog(#"Button Tag=%d",Button.tag);
if([[self.MySectionIndexArray objectAtIndex:Button.tag] isEqualToString:#"UP"])
{
[self.MySectionIndexArray replaceObjectAtIndex:Button.tag withObject:#"DOWN"];
[ButtonDrop setBackgroundImage:[UIImage imageNamed:#"Up.png"] forState:UIControlStateNormal];
}
else
{
[self.MySectionIndexArray replaceObjectAtIndex:Button.tag withObject:#"UP"];
[ButtonDrop setBackgroundImage:[UIImage imageNamed:#"Down.png"] forState:UIControlStateNormal];
}
[TableDashBoard reloadSections:[NSIndexSet indexSetWithIndex:Button.tag] withRowAnimation:UITableViewRowAnimationFade];
}
Try having a zero-height row for your empty sections. That seems to make them draw correctly for me.
I think there are a couple of explainations:
(1) You're displaying the section header of an empty section which would cause two headers to appear one on top of the other with no rows in between.
(2) You are returning a header view as a row cell in – tableView:cellForRowAtIndexPath: Since the header "it takes place of a row in tableview" I think this the most likely explanation. Check – tableView:viewForHeaderInSection: as well.