I have a UITableView with variable height cells. I would prefer not to have to use a background image, but rather would like to set a backgroundView with the styling that I want. Currently, I can't figure out how to dynamically change the height of my backgroundView based on the height of the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 60)];
view.backgroundColor = [UIColor whiteColor];
view.layer.cornerRadius = 2.0;
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOffset = CGSizeMake(0, 1);
view.layer.shadowRadius = 0.4;
view.layer.shadowOpacity = 0.2;
[cell.contentView addSubview:view];
[cell.contentView sendSubviewToBack:view];
}
ZSSLog *log = [self.items objectAtIndex:indexPath.row];
cell.textLabel.text = log.logText;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:15.0];
cell.textLabel.textColor = [UIColor grayColor];
return cell;
}
Right now the background views just overlap since they are not being resized:
Is this possible?
Instead of setting the frame of your background view you might want to do something like
UIView *view = [[UIView alloc] initWithFrame:cell.contentView.bounds];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
This answer assumes you're not using auto-layout since you're setting the frame of your background view. If you're using auto-layout you don't want to be setting the frame at all and instead setting constraints on your background view.
Normally, you'd use the backgroundView property for this. Try setting up your background view like this:
UIView *view = [[UIView alloc] initWithFrame:cell.bounds];
view.frame = UIEdgeInsetsInsetRect(view.frame, UIEdgeInsetsMake(0, 10, 0, 10));
view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
//...other cell config...
cell.backgroundView = view;
But if you really want to put this view inside the contentView, you can do that like:
UIView *view = [[UIView alloc] initWithFrame:cell.contentView.bounds];
view.frame = UIEdgeInsetsInsetRect(view.frame, UIEdgeInsetsMake(0, 10, 0, 10));
view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
//...other cell config...
[cell.contentView addSubview:view];
Related
I am defining a header row for my UITableView using the viewForHeaderInSection delegate method, and I wanted to get the X coodinate from the first row to position some labels, because I'm moving about the labels in the row constantly and would like to define those coordinates in the storyboard. But it's not working: the X coordinate is always 0. Here is the attempt:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
float x = tableView.frame.origin.x;
float y = tableView.frame.origin.y;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y - 22, tableView.bounds.size.width, 12)];
view.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
view.userInteractionEnabled = YES;
view.backgroundColor = [UIColor lightGrayColor];
LogCell *cell = (LogCell *)[tableView cellForRowAtIndexPath:0];
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.label_DATE.frame.origin.x, 0, 65, 22)];
dateLabel.backgroundColor = [UIColor clearColor];
dateLabel.font = [UIFont systemFontOfSize:12.0];
dateLabel.text = #"DATE";
UILabel *locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.label_LOC.frame.origin.x, 0, 65, 22)];
locationLabel.backgroundColor = [UIColor clearColor];
locationLabel.font = [UIFont systemFontOfSize:12.0];
locationLabel.text = #"LOC";
[view addSubview:dateLabel];
[view addSubview:locationLabel];
return view;
}
Is there some way to properly get a row from the UITableView to ascertain the X/Y coordinates of the cell's labels?
This line:
LogCell *cell = (LogCell *)[tableView cellForRowAtIndexPath:0];
is the same as:
LogCell *cell = (LogCell *)[tableView cellForRowAtIndexPath:nil];
You aren't passing in an index path:
Try this:
LogCell *cell = (LogCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
I'm confused !, it is not my first time to create Custom Cell but this time UILabel doesn't appear I don't know where is the problem ?
This the custom cell Cell.m
mainView = [[UIView alloc] initWithFrame:CGRectZero];
mainView.backgroundColor = [UIColor colorWithRed:249.0/255.0 green:249.0/255.0 blue:249.0/255.0 alpha:1.0];
profile = [[UIImageView alloc] initWithFrame:CGRectZero];
profile.layer.masksToBounds = YES;
profile.layer.cornerRadius = 22.5;
profile.layer.borderColor = [UIColor whiteColor].CGColor;
profile.layer.borderWidth = 1.0f;
profile.clipsToBounds = YES;
tweet_is = [[UILabel alloc] initWithFrame:CGRectZero];
tweet_is.font = [UIFont fontWithName:#"HelveticaNeue-Thin" size:20];
tweet_is.backgroundColor = [UIColor clearColor];
tweet_is.textColor = [UIColor blackColor];
[self.mainView addSubview:tweet_is];
[self.mainView addSubview:profile];
[self.contentView addSubview:self.mainView];
-(void)layoutSubviews {
[super layoutSubviews];
self.mainView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 10.0f);
self.profile.frame = CGRectMake(15, 15, 45, 45);
self.tweet_is.frame = CGRectMake(30, 30, 300.0f, 300.0f);
}
This is the tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.tweet_is.text = #"Not approve";
return cell;
}
Change this line:
self.tweet_is.frame = CGRectMake(30, 30, 300.0f, 300.0f);
set it self.tweet_is.frame = CGRectMake(30, 30, 300.0f, 30.0f);
If your cell height is less than 300 (I think it is) then you can not see the text. Let me know if that helps.. :)
First screenshot is iOS7 that not what I want.
First screenshot is iOS6 that what I want.
Tableview's style is plain.
Tableview's separator is none.
And there is a backgroudView of that darkgray color.
I have code like below
if ([tableView respondsToSelector:#selector(setSeparatorInset:)])
{
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"icon_bg_box.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
You need to add separate view as a seperator
First make tableViews seperator to none
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
[cell addSubview:[self drawSeparationView:(indexPath.row)]];
return cell;
}
Then draw your seperator
- (UIView*)drawSeparationView:(NSInteger)itemNo {
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(0, 0, self.tableView.frame.size.width, cellHeight);
UIView *upperStrip = [[UIView alloc]init];
upperStrip.backgroundColor = [UIColor colorWithWhite:0.138 alpha:1.000];
upperStrip.frame = CGRectMake(0, 0, view.frame.size.width, 2);
[view addSubview:upperStrip];
UIView *lowerStrip = [[UIView alloc]init];
lowerStrip.backgroundColor = [UIColor colorWithWhite:0.063 alpha:1.000];
lowerStrip.frame = CGRectMake(0, cellHeight-2, view.frame.size.width, 2);
[view addSubview:lowerStrip];
return view;
}
The output will be something like this
This will hide the separator
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
Then add your custom separator imageView in every cell at bottom.
Try this
self.tableview.separatorColor = [UIColor clearColor];
If you want to remove the separator line of tableviewcell
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
Then add separator line for custom cell
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];/// change size as you need.
separatorLineView.backgroundColor = [UIColor grayColor];// you can also put image here
[cell.contentView addSubview:separatorLineView];
Credits go to iPatel Answer
I made custom cell with scrollViewon right side of the cell and imageView on left side. Now when I click on imageView I get correct value for indexPath.row, but when I click on scrollView indexPath.row is returning value from last image that was clicked. Any suggestions how to fix this?
EDIT:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *hlCellID = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:hlCellID];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:hlCellID];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
UIScrollView *scrollView = (UIScrollView *)[cell viewWithTag:16];
scrollView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width,scrollView.frame.size.height);
[scrollView setShowsHorizontalScrollIndicator:NO];
[scrollView setShowsVerticalScrollIndicator:NO];
int i=0;
for(i=0;i<15;i++){
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0+i*100, 0, 100, 100)];
label.text = #"HELLO";
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont fontWithName:#"ArialMT" size:18];
[scrollView addSubview:label];
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width+i*label.frame.size.width,scrolView.frame.size.height);
}
return cell;
}
UIScrollView *scrollView = (UIScrollView *)[cell viewWithTag:16];
Change above line with following.
UIScrollView *scrollView = (UIScrollView *)[cell viewWithTag:indexPath.row];
i hope this works for you..
You should look at this doc reference: responder chain
Basically your event doesn't goes uphill the responder chain because scroll view captures it. I don't know what you are trying to achieve but having scrollview inside a scrollview(because tables are scrollviews by their nature) is bad idea and is really hard to achieve.
I have a UITableView that has an image and text on each cell, however they are put side by side. What I'm trying to do is put the text label over the top of the image. Is it possible?
Take custom UITableViewCell,where take UIView at every cell and make your image as the background color of the UIView then take a UILabel at the top of the UIView.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
cell.textLabel.font = [UIFont fontWithName:#"Arial" size:15.0];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pl_arows.png"]];
cell.accessoryView = imageView;
cellView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,290, 70)] autorelease]; //important do not changer the position
cellView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"order_description_bg.png"]];
cellView.tag =10;
[cell.contentView addSubview:cellView];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 15, 39, 36)];
imgView.image = [UIImage imageNamed:#"image_category.png"];
imgView.layer.borderColor = [UIColor blackColor].CGColor;
imgView.tag = 5;
[cellView addSubview:imgView];
//Status label
CGRect statusRectText = CGRectMake(52, 0, 50, 18);
UILabel *statusLabelText = [[[UILabel alloc] initWithFrame:statusRectText] autorelease];
statusLabelText.textAlignment = UITextAlignmentRight;
statusLabelText.backgroundColor = [UIColor clearColor];
statusLabelText.tag = 101;
[imgView addSubview:statusLabelText];
}
You will have to define a custom UITableViewCell in which you can place any standard UI objects, including a UILabel on top of your UIImage.