How to get UITableView's FooterView to be visible? - objective-c

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.

Related

Set UILabel text from UITableView header section

I've got an sectioned UITableView and want to set a label (not in the tableview) the text of the label that's on top.
I tried settings label with [label setText:#"name"]; in
(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
but that didn't work. Anyone else have ideas how to do this?
Try this
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 60)];
headerView.backgroundColor = [UIColor clearColor];
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0,0, 50, 50)];
label.backgroundColor = [UIColor yellowColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
[headerView addSubview:label];
return headerView;
}
If you're using that method you only need to return the string. It will handle creating a label for you.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return #"Name";
}
If you want to use your own UIView or UILabel you'll need to use a different dataSource method.
- (UIView *) tableview:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *customLabel = [[UILabel alloc] initWithFrame:CGRect(0.0,0.0,100.0,20.0)];
[customLabel setText:#"name"];
return customLabel;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
self.tableView1.tag = 1;
if ( [tableView tag] == 1 ) {
return #"TableView One Title";
}
return #"other Tableview Title";
}

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.

Adding a UISegmentedControl in UITableView's header

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.

UIView background color does not take effect

Can someone tell me why this is not working right?
I have these lines of code below within my table view cell for tableView:didSelectAtIndexRowPath: method.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[tableView viewWithTag:199]removeFromSuperview];
CGSize cellSize = [tableView cellForRowAtIndexPath:indexPath].frame.size;
UIView *subSelectionView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, (int)cellSize.width - 20, 100)];
[subSelectionView setBackgroundColor:[UIColor blueColor]];
subSelectionView.layer.borderColor = [UIColor grayColor].CGColor;
subSelectionView.layer.borderWidth = 1;
subSelectionView.tag = 199;
[[tableView cellForRowAtIndexPath:indexPath]addSubview:subSelectionView];
}
Notice the code:
[subSelectionView setBackgroundColor:[UIColor blueColor]];
Obviously, I want to change the color of the subview that I added to the UITableViewCell but why is it not working ?
jus reload the table after adding the subview on the table
[[tableView cellForRowAtIndexPath:indexPath]addSubview:subSelectionView];
[tableview reloadData];
Add the following line to the start of the method
[tableView deselectRowAtIndexPath:indexPath animated:NO];
So ur method would be
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[[tableView viewWithTag:199]removeFromSuperview];
....
}

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;
}