Set UILabel text from UITableView header section - objective-c

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

Related

Add image to a section in UITableView in Objective C

I know how to add an image to a cell in a UITableView, but how can I had an image the same way to the section title ?
Same way as you creates and returns cells for tableView's requests you can create and configure view for section header via
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,CGRectGetWidth(tableView.frame), DESIRED_HEADER_HEIGHT)];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithName:#"imageName"]];
imageView.frame = /*set image where you want*/;
[headerView addSubview: imageView]
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return DESIRED_HEADER_HEIGHT;
}
You can override UITableViewDelegate method
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
create an UIImageView with the required image and return it.
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"img"]];
}

UItableview is moving up and down rather than scrolling inside

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

UITableView willDisplayCell Acting Weird, Cells borders adjusted only after reuse

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.

updateSwitchAtIndexPath crashes

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.

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