Adding a UISegmentedControl in UITableView's header - objective-c

I am using the following code to add a UISegmentedControl in UITableView. Everything works fine except that the UISegmentedControl is not responding to user interaction at all. What could be the matter?
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(section == 2) {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)]; // x,y,width,height
NSArray *itemArray = [NSArray arrayWithObjects: #"One", #"Two", nil];
UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:itemArray];
[control setFrame:CGRectMake(60.0, 0, 200.0, 40.0)];
[control setSegmentedControlStyle:UISegmentedControlStylePlain];
[control setSelectedSegmentIndex:0];
[control setEnabled:YES];
[headerView addSubview:control];
return headerView;
}
}

You should also have a corresponding heightForHeaderInSection method that looks like this:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 2) return 44.f;
return 0.f;
}
If not, the control may still appear, but won't be drawn within any touchable area.

Related

How to get UITableView's FooterView to be visible?

When I set the tableview's footerView like this :
self.tableView.tableFooterView.frame = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 45.0f);
then my cellCount*cellHeight > tableview.frame.size.height.
then I find that footerView can't be show. Must to pull the tableview that it show. How to solve it?
self.tableView.tableFooterView = [[UIView alloc] init];
self.tableView.tableFooterView.backgroundColor = [UIColor orangeColor];
[self.tableView setEditing:YES animated:YES];
self.tableView.tableFooterView.frame = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 45.0f);
//It can't to solve My Problem
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:9 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
Create footerView (by code or in xib). Then use following delegate methods:
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return footerView.frame.size.height;
}
- (UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
return footerView;
}
In your case you might not be returning the height for footer.

Unable to set the frame of UITableViewCell's contentView

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.

How to set footer in one section of TableView manually (iOS)?

I would like to implement some code, which changes footer text in one section of the tableView (in viewDidAppear or viewWillAppear method). But how can I do it?
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
doesn't fit my requirements (It changes only once, during load of the tableView, but I need to change the footer's text after text in tableView cell is changed.
-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 120;
}
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return #"Things We'll Learn";
} else {
return #"Things Already Covered";
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[tableView reloadData];
}
Implement viewForFooterInSection and add your textField there. Also make that textField a property.
When you have finished editing you tableViewCells, implement the textFieldDidEndEditing method and assign necessary value to the textField of your footerView.
Once your textField is set, use [tableView reloadData] to implement the viewForFooterInSection again and it should work now.
Edit:
If you want to change the title of the Footer section after editing the UITableViewCell,
Set a global variable or use NSUserDefaults to indicate that tableViewCell has been edited.
self.tableView reloadData right after edit.
In the method titleForFooterInSection check for that variable (this would mean that tableView has been edited) and set the title accordingly.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if(section == 1)
{
// For Lable
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 40)] autorelease];
tableView.sectionHeaderHeight = view.frame.size.height;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, view.frame.size.width - 20, 44)];
label.text = [self tableView:tableView titleForHeaderInSection:section];
label.font = [UIFont boldSystemFontOfSize:16.0];
label.shadowOffset = CGSizeMake(0, 1);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.adjustsFontSizeToFitWidth = YES;
[label setLineBreakMode:NSLineBreakByTruncatingTail];
[label setNumberOfLines:0];
label.text = #“Your Text Here…..your Text Here”;
[view addSubview:label];
[label release];
return view;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if(section == 1)
{
return 60.0;
}
return 0;
}

Logic for adding images and scroll icon to the table view

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!!

Section headerview in UITableView Duplicated

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.