Add image to a section in UITableView in Objective C - 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"]];
}

Related

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

MKMapView inside a UITableViewCell

I have a UITableView with UITableViewCells that contains a MKMapView.
The Problem: if the table cell ever gets selected, then move the map, you see the mapview is all white and you only see the "Legal" label.
Has anyone experienced this before?
Here is the entire code:
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.table.backgroundColor = [UIColor clearColor];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MKMapView *map = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 220, 70)];
MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
CLLocationCoordinate2D logCord = CLLocationCoordinate2DMake(47.606, -122.332);
MKCoordinateRegion region = MKCoordinateRegionMake(logCord, span);
[map setRegion:region animated:NO];
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"aaaa"];
[cell.contentView addSubview:map];
return cell;
}
I have experienced this.
In my case, the map becomes white like this if its tableView or cell has a backgroundColor and its cell has either a gray or blue selection style and the cell is recycled through dequeue.
I think that the map won't turn white like this if
cell.selectionStyle = UITableViewCellSelectionStyleNone;
I have experienced this as well.
My map had white background when it was touched after pushing/popping another detail view controller. TableView has no color/background configured yet, everything is default.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
...actually helped.

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.

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