I have a table view, and I am adding four UIImageViews to a row, so in total I have five rows and twenty image views. I am inserting images one by one into the cell. The second row will only start to fill once all four image views are filled.
I need some logic for this. I mean, how can I check which place is next to be filled in the table view and at which event? How can I add images one by one to the UIImageViews? Also, initially only two rows will be shown. After filling these two rows, when images begin to enter into the third row, I need to show an icon adjacent to the last row that is on the screen, to tell the user that there is more data below. Also, is there any event to detect which row is currently the last row on screen?
Also is there any way to individually select the images from a row?
Run this code:-
.h file code
#import <UIKit/UIKit.h>
#interface helpTableViewController : UIViewController {
IBOutlet UITableView *gridTableView;
NSMutableArray *imagesArray;
int widthview,widthview1;
}
#property(nonatomic,retain)NSMutableArray *imagesArray;
#property(nonatomic,retain)UITableView *gridTableView;
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath;
-(void)crossViewButtonClicked:(UIButton *)sender;
#end
.m file code:-
#synthesize gridTableView;
#synthesize imagesArray;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *tempArray=[[NSMutableArray alloc]init];
self.imagesArray=tempArray;
[tempArray release];
[self.imagesArray addObject:[UIImage imageNamed:#"pic1.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic2.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic3.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic4.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic5.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic6.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic1.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic2.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic3.png"]];
widthview=0;
widthview1=0;
}
- (void)viewDidUnload {
self.gridTableView=nil;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[imagesArray release];
[gridTableView release];
[super dealloc];
}
#pragma mark -
#pragma mark TableView Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=nil;
if (cell == nil) {
cell =[self reuseTableViewCellWithIdentifier:CellIdentifier withIndexPath:indexPath];
}
return cell;
}
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];
if (indexPath.row==0) {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
scrollView.contentSize = CGSizeMake(320, 180);
scrollView.backgroundColor=[UIColor clearColor];
scrollView.showsHorizontalScrollIndicator = NO;
[cell.contentView addSubview:scrollView];
for (int i=0; i<4;i++) {
UIView *profileView=[[UIView alloc]initWithFrame:CGRectMake(widthview+10, 12, 95, 155)];
profileView.backgroundColor=[UIColor whiteColor];
UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 8, 83, 106)];
[profileimageView setImage:[self.imagesArray objectAtIndex:i]];
profileimageView.tag=i+90;
[profileView addSubview:profileimageView];
[profileimageView release];
[scrollView addSubview:profileView];
widthview=widthview+105;
NSLog(#"%d",widthview);
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(widthview1+10, 12, 95, 155)];
//button.contentMode = UIViewContentModeScaleAspectFill;
button.tag=999999;
[button setBackgroundColor:[UIColor greenColor]];
[button addTarget:self action:#selector(imageButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
widthview=widthview+105;
scrollView.contentSize = CGSizeMake(widthview, 180);
}
if (indexPath.row==1) {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
scrollView.contentSize = CGSizeMake(320, 180);
scrollView.backgroundColor=[UIColor clearColor];
scrollView.showsHorizontalScrollIndicator = NO;
[cell.contentView addSubview:scrollView];
for (int i=0; i<4;i++) {
UIView *profileView=[[UIView alloc]initWithFrame:CGRectMake(widthview1+10, 12, 95, 155)];
profileView.backgroundColor=[UIColor whiteColor];
UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 8, 83, 106)];
// [profileimageView setImage:[UIImage imageNamed:#"gridimage1.png"]];
profileimageView.tag=4+i;
[profileimageView setImage:[self.imagesArray objectAtIndex:3+i]];
[profileView addSubview:profileimageView];
[scrollView addSubview:profileView];
[profileimageView release];
widthview1=widthview1+105;
NSLog(#"%d",widthview1);
}
scrollView.contentSize = CGSizeMake(widthview1, 180);
}return cell;
}
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
#pragma mark -
#pragma mark button methods
-(void)imageButtonClicked:(UIButton *)sender
{
[self.imagesArray replaceObjectAtIndex:0 withObject:[UIImage imageNamed:#"abdulprofilepic.png"]];
[self.gridTableView reloadData];
}
You should keep all of the logic (and data) in your table view controller (UITableViewController subclass). I suggest you have a custom UITableViewCell subclass that can hold 4 images (the cell itself should be responsible for the layout), and the table view controller is responsible for setting it up. In -tableView:cellForRowAtIndexPath: you can look at your collection of images, then just use modular arithmetic to figure out which images should be in the cell for that particular index path.
As for individually selecting images, perhaps you could have your cells know their index path (set as a custom property in -tableView:cellForRowAtIndexPath:), then each have a delegate method telling your controller that a certain image was pressed (for example, -cellAtIndexPath:didSelectImageAtIndex:).
try this,
As there are fix number of images that you want to show in your row i. e. 4
So what you have to do is:-
in .h file
int widthview,widthview1;
in .m file in viewdidload
widthview=0;
widthview1=0;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [self reuseTableViewCellWithIdentifier:CellIdentifier withIndexPath:indexPath];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;//to remove blu selection color
return cell;
}
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];
if (indexPath.row==0) {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
scrollView.contentSize = CGSizeMake(320, 180);
scrollView.backgroundColor=[UIColor clearColor];
scrollView.showsHorizontalScrollIndicator = NO;
[cell.contentView addSubview:scrollView];
for (int i=0; i<4;i++) {
UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(widthview+10, 12, 95, 155)];
[profileimageView setImage:[self.imagesArray objectAtIndex:i]];
[profileimageView release];
[scrollView addSubview:profileimageView];
widthview=widthview+105;
NSLog(#"%d",widthview);
}
scrollView.contentSize = CGSizeMake(widthview, 180);
}
if (indexPath.row==1) {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
scrollView.contentSize = CGSizeMake(320, 180);
scrollView.backgroundColor=[UIColor clearColor];
scrollView.showsHorizontalScrollIndicator = NO;
[cell.contentView addSubview:scrollView];
for (int i=0; i<4;i++) {
UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(widthview+10, 12, 95, 155)];
[profileimageView setImage:[self.imagesArray objectAtIndex:4+i]];
[profileimageView release];
[scrollView addSubview:profileimageView];
widthview1=widthview1+105;
NSLog(#"%d",widthview1);
}
scrollView.contentSize = CGSizeMake(widthview1, 180);
}
return cell;
}
This code is for 2 rows do the same thing for next three rows.
Adjust the coordinates according to your requirement.
hope this will save your time.
answer for Also is there any way to individually select the images from a row? :
Give image to button and add that button inside row!!
Related
I have a UITableView with about 15 cells and setScrollEnabled:YES. The problem I am facing is this:
When I scroll, the entire table view moves. I.e.: If I scroll up, the table view moves up and covers the top bar.
What I'd like to do is to scroll within the inside of UITableView so it doesn't move, only the cells/cell content within it move up/down (possibly even the use of a vertical indicator as well?).
I can't find anything on SO or a Google Search.
What I'm currently using is as follows:
#define NUMBER_ROWS 15
- (void)viewDidLoad
{
[super viewDidLoad];
if ([self.rearTableView respondsToSelector:#selector(setSeparatorInset:)]) {
[self.rearTableView setSeparatorInset:UIEdgeInsetsZero];
}
if (IS_WIDESCREEN) {
[self.rearTableView setScrollEnabled:YES];
[self.rearTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return NUMBER_ROWS;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
MenuCell *cell = (MenuCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (nil == cell)
{
cell = [[MenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
[cell.textLabel setHighlightedTextColor:[UIColor whiteColor]];
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds];
[cell.selectedBackgroundView setBackgroundColor:[UIColor menuSelected]];
if (indexPath.row == 0)
{
cell.textLabel.text = #" Films";
cell.imageView.image = [UIImage imageNamed:#"icon_film.png"];
cell.imageView.highlightedImage = [UIImage imageNamed:#"icon_film_active.png"];
[cell.contentView setBackgroundColor:[UIColor menuGray]];
[cell.textLabel setTextColor:[UIColor menuParent]];
//[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[[cell textLabel] setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:18]];
}
....
}
Images of the issue:
UITableView moving down
UITableView moving up and covering the top bar
Thanks
I am trying to have 10 pixels gap between cells and the edge of the screen. I implemented willDisplayCell:forRowAtIndexPath and also the heightForCell since am using two different prototype cells in my table view.
The problem am having is really weird. The cells are only adjusted when i pull the list up, am guessing only when they are reused! check the images please.![enter image description here][1]
This is the initial screen:
https://www.dropbox.com/s/epl2n8a5zohhdmz/Edited.screenShot.01.png?dl=0
This is the screen after the table view is scrolled:
https://www.dropbox.com/s/c81dx9wmaq0mgr5/Edited.screenShot.02.png?dl=0
Here is my code:
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.layer.borderWidth = 5.0f;
cell.layer.borderColor = [UIColor grayColor].CGColor;
cell.layer.cornerRadius = 7.0;
cell.layer.bounds = CGRectMake(0, 0, 260, cell.bounds.size.height - 10);
cell.backgroundColor = [UIColor colorWithRed:207/255.0 green:207/255.0 blue:207/255.0 alpha:1.0];
CGRect cellFrame = CGRectMake(20, cell.frame.origin.y, 280, cell.frame.size.height);
cell.frame = cellFrame;
cell.contentView.frame = cellFrame;
[cell.contentView alignmentRectForFrame:cellFrame];
[cell setNeedsLayout];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[self.namesList[indexPath.row] objectForKey:#"Relation"] length] == 0) {
return 69.0;
} else {
return 89.0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableString *details = [[NSMutableString alloc] init];
UITableViewCell *cell = [[UITableViewCell alloc] init];
UILabel *nameLabel;
UILabel *relationLabel;
UILabel *ageLabel;
UILabel *dayLabel;
UILabel *dateLabel;
Helper *dateConvert = [[Helper alloc] init];
if ([[self.namesList[indexPath.row] objectForKey:#"Relation"] length] == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell2" forIndexPath:indexPath];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell3" forIndexPath:indexPath];
relationLabel = (UILabel *) [cell viewWithTag:2];
relationLabel.text = [self.namesList[indexPath.row] objectForKey:#"Relation"];
}
nameLabel = (UILabel *) [cell viewWithTag:1];
ageLabel = (UILabel *) [cell viewWithTag:3];
dayLabel = (UILabel *) [cell viewWithTag:4];
dateLabel = (UILabel *) [cell viewWithTag:5];
[details appendString:[self.namesList[indexPath.row] objectForKey:#"Age"]];
[details appendString:#")"];
ageLabel.text = details;
dayLabel.text = [dateConvert getWeekDay:[self.namesList[indexPath.row] objectForKey:#"Date"]];
dateLabel.text = [self.namesList[indexPath.row] objectForKey:#"Date"];
return cell;
}
I have spent days trying to find the problem :( every help is greatly appreciated :)
Why not make your life easier and design the cell in Interface Builder to have the 10 px space already. You can add a UIView, make that 10 px from top, left, right and bottom, then put you UI elements inside of that. I would suggest subclassing your UITableViewCells and have code in those to display your data and set the borders.
Im trying to set an offset of the contentView of my UITableViewCell. But failed. As you can find in the picture, there is no offset.
The corresponding code is as following.
MyCell.m
#import "MyCell.h"
#implementation MyCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
[backGroundView setBackgroundColor:[UIColor grayColor]];
[self addSubview:backGroundView];
UIView *frontView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
[frontView setBackgroundColor:[UIColor greenColor]];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
myLabel.text = #"My Cell";
myLabel.backgroundColor = [UIColor clearColor];
[myLabel setTextAlignment:UITextAlignmentCenter];
[frontView addSubview:myLabel];
[self.contentView addSubview:frontView];
[self bringSubviewToFront:self.contentView];
}
return self;
}
MyTableViewController.m
#import "MyTableViewController.h"
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
CGPoint center = cell.center;
center.x = cell.contentView.bounds.size.width/2 - 20;
[cell.contentView setCenter:center];
NSLog(#"origin.x :%f ,y :%f, width :%f, height :%f",cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width,cell.contentView.frame.size.height);
return cell;
}
log
2012-08-31 18:29:34.075 TableViewCellOffsetTest[16169:207] origin.x :-20.000000 ,y :0.000000, width :320.000000, height :44.000000
Show us your code relating to [cell.contentView setCenter:center]; I suspect you're trying to move the main view of the cell. I wouldn't do that I would add a subview and offset that and add your content views to it. As you're already using a custom method all your changes need to be made there. In addition it's worth noting that you would also want to reset the cells offset each time it's reused as you'll end up moving the content further and further along.
In my app i am having a pop over controller which shows a bunch of images when clicked from other view.The other view have some images in it.All the images have a arrow button above them clicking on which the pop over gets displayed.let me come to the issue.say e.g i clicked the arrow button above the second image and pop over gets displayed .Now when i select any image from the pop over then i want to set that image to the image view present in the main view(or the image view which consist the arrow button).But what i am getting is that when i select any image from the pop over then it always gets set to the last image view present in the main view.I want to set it on the same image view above whose arrow button is clicked to display the pop over.
my code is:-
main view:--
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = #"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
UIImageView * imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 80, 80)] autorelease];
UIImageView * imageView2 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,80, 80)] autorelease];
UIImageView * imageView3 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 80, 80)] autorelease];
UIImageView * imageView4 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 80, 80)] autorelease];
imageView1.tag = j;
imageView2.tag = j+1;
imageView3.tag = j+2;
imageView4.tag = j+3;
[cell.contentView addSubview:imageView1];
[cell.contentView addSubview:imageView2];
[cell.contentView addSubview:imageView3];
[cell.contentView addSubview:imageView4];
}
for ( int i = 1; i <= j; i++ ) {
imageView = (UIImageView *)[cell.contentView viewWithTag:i];
imageView.image=nill
}
int photosInRow;
if ( (indexPath.row < [tableView numberOfRowsInSection:indexPath.section] - 1) || ([sentence count] % 4 == 0) ) {
photosInRow = 4;
} else {
photosInRow = [sentence count] % 4;
}
for ( int i = 1; i <=[sentence count]; i++ ){
imageView = (UIImageView *)[cell.contentView viewWithTag:i];
[self setImage1:imageView];
}
return cell;
}
Click event of the button:--
-(void)showPopOver:(id)sender
{
ModalView *mvc = [[ModalView alloc]init];
[mvc settingParentButton:imageView];
[mvc settingPop:detailViewPopover];
// Present the popover from the button that was tapped in the detail view.
[detailViewPopover presentPopoverFromRect:CGRectMake(0,0, 325, 650) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
// Set the last button tapped to the current button that was tapped.
[mvc release];
}
setting parent button event of modal view:--
-(void)settingParentButton:(UIImageView *)imageView {
imageView1=imageView;
}
and did select row at index path of modal view:-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(imageView1.tag);
if (indexPath.row==0) {
[imageView1 setImage:[UIImage imageNamed:[nsmResult objectAtIndex:indexPath.row]] ];
}
if (indexPath.row==1) {
[imageView1 setImage:[UIImage imageNamed:[nsmResult objectAtIndex:indexPath.row]] ];
}
if (indexPath.row==2) {
[imageView1 setImage:[UIImage imageNamed:[nsmResult objectAtIndex:indexPath.row]] ];
}
if (indexPath.row==3) {
[imageView1 setImage:[UIImage imageNamed:[nsmResult objectAtIndex:indexPath.row]] ];
}
[pop2 dismissPopoverAnimated:YES];
}
Please help.Is it the tag issue ,please tell how can i check which tag is now in progress through which i can get the image view.Please answer.
In that imageview while creating the memory allocation,autorelease should not be given,realease your memory in the dealloc, your expected image wil be placed in your occurences.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *im = [info objectForKey:UIImagePickerControllerOriginalImage];
self.imageView.image = im;
UIImageWriteToSavedPhotosAlbum(im, nil, nil, nil);
imageView.contentMode = UIViewContentModeScaleAspectFit;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
#interface testpViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
IBOutlet UIImageView *imageView;
}
#property (nonatomic, retain) UIImageView *imageView;
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.