how to stop custom UITableViewCell covering UITableView border lines - ios

Perhaps this is a very simple answer, but I have searched everywhere and it seems everyone is trying to do the opposite of what I am trying to do. I have a custom UITableViewCell as defined below.
#import "TagCell.h"
#import "TagRepository.h"
#import "TagsViewController.h"
#implementation TagCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self setFrame:CGRectMake(0, 1, self.frame.size.width, 37)];
self.selectionStyle= UITableViewCellSelectionStyleNone;
self.textLabel.hidden=NO;
[self.textLabel setFrame:CGRectMake(self.bounds.size.width/2-40, self.bounds.origin.y+1, 300, 30)];
self.textLabel.text= #"test";
[self.textLabel setFont:[UIFont fontWithName:#"Helvetics" size:30] ];
}
return self;
}
-(void) layoutSubviews
{
NSArray *viewsToRemove = [self subviews];
for (UIView *v in viewsToRemove) {
if([v isKindOfClass:[UIButton class]])
{
[v removeFromSuperview]; //used to remove and redraw the button during a orientation change
}
}
UIButton * removeButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[removeButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:18] ];
[removeButton setFrame:CGRectMake(7*self.bounds.size.width/8, self.bounds.origin.y+3, 30, 30)];
removeButton.hidden=NO;
[removeButton addTarget:self action:#selector(removeTag) forControlEvents:UIControlEventTouchUpInside];
[removeButton addTarget:self action:#selector(removeTag) forControlEvents:UIControlEventTouchDragInside];
[removeButton setBackgroundImage:[UIImage imageNamed:#"button_red_unselect.png"] forState:UIControlStateNormal];
[removeButton setBackgroundImage:[UIImage imageNamed:#"button_red_select.png"] forState:UIControlStateSelected];
[removeButton setImage:[UIImage imageNamed:#"icon_minus.png"] forState:UIControlStateNormal];
[removeButton setImage:[UIImage imageNamed:#"icon_minus.png"] forState:UIControlStateSelected];
[self addSubview:removeButton];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void) removeTag
{
for( int i=0 ; i< [TagRepository instance].tags.count; i++)
{
if([self.textLabel.text isEqualToString:[TagRepository instance].tags[i]])
{
[[TagRepository instance].tags removeObjectAtIndex:i] ;
}
}
_reloadTable();
// [_delegate.table reloadData];
//RELOAD TABLE
}
#end
So what happens is, that when I open up the tableview when there is an active custom cell the border is not present and it appears as if there is a blank cell following it.. or something.
This is not desired I wish the cell had a border surrounding it.
The view that contains the table sets heightforcellatindex:indexpath = 40, so that delegate function returns 40 as the cell height value, I assume that the problem lies between some sort of mismatch between this and the subviews in the custom cell. Do you see anything wrong with this??
note that I have already changed the typo in Helvetica.

If you add [super layoutSubviews] to your layoutSubviews method, the cell separators will appear. I'm not sure what you mean by the blank cell following it -- I didn't see anything like that when I tried your custom cell. Also, you should be adding (and removing) your buttons to the cell's contentView, not to the cell itself.

Related

UIButton in a subview not response to touch events with objective-c

I want to create a bottom tab that could change the view when user press.
To do that I create a view with view controller and sets it's frame in the init
Here is the bottom panel view controller
#implementation BottomTabViewController
-(id) initWithFrame:(CGRect)frame{
if(self = [super init]){
self.view.frame = frame;
return self;
}else{
return nil;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpButtons];
// Do any additional setup after loading the view.
}
-(void) featureBtnClick:(id*) sender{
NSLog(#"featureBtn Clicked");
}
-(void) favBtnClick:(id*)sender{
NSLog(#"Fav Btn Clicked");
}
-(void) setUpButtons{
UIButton *features = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
[features setBackgroundImage:[UIImage imageNamed:#"feature.png"] forState:UIControlStateNormal];
features.backgroundColor = [UIColor greenColor];
[features addTarget:self action:#selector(featureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:features];
UIButton *favBtn = [[UIButton alloc]initWithFrame:CGRectMake([[UIScreen mainScreen]bounds].size.width - 100, 10, 50, 50)];
[favBtn setBackgroundImage:[UIImage imageNamed:#"favorite.png"] forState:UIControlStateNormal];
[favBtn addTarget:self action:#selector(favBtnClick:) forControlEvents:UIControlEventTouchUpInside];
favBtn.backgroundColor = [UIColor greenColor];
[self.view addSubview:favBtn];
}
#end
And I added that to my view with code like this:
-(void) setUpBottom{
BottomTabViewController *botm = [[BottomTabViewController alloc]initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 55, [[UIScreen mainScreen]bounds].size.width, 55)];
botm.view.backgroundColor = [UIColor redColor];
// botm.view.userInteractionEnabled = false;
botm.view.userInteractionEnabled = true;
[self.view addSubview:botm.view];
}
Here here is the result seems, note the bottom panel that works, well pretty silly, but it shows up:
But when press the left and right button, there is not log printed which it expected to be to indict that the button works, What have I done wrong? Thanks
you are adding buttons in the view first then adding the bottom view.
if this is what you are doing then it can be overlapped by the bottom view.
In this case buttons cant be tapped because they have bottom view on top of them.
so make sure u add bottom view first then your buttons. (or change their frames to avoid overlapping)

MLKMenuPopover in UITableViewCell not working

I am using MLKMenuPopover in UITableViewCell not working and it's not showing correctly and also not working
There is line on the menu of cell separator
This is the code for creating the round button for popup in custom UITableViewCell
_btnDelete=[UIButton buttonWithType:UIButtonTypeCustom];
[_btnDelete setFrame:CGRectMake(DeviceWidth-30, 80, 20, 20)];
[_btnDelete setBackgroundImage:[UIImage imageNamed:#"edit_icon.png"] forState:UIControlStateNormal];
[_btnDelete addTarget:self action:#selector(btnRemoveClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:_btnDelete];
And this action is also custom UITableViewCell
-(void)btnRemoveClicked:(id)sender{
if (self.delegate!=nil && [self.delegate respondsToSelector:#selector(delegatebtnAddressRemoveClicked:)]) {
[self.delegate delegatebtnAddressRemoveClicked:sender];
}
}
And here is the delegate method
-(void)delegatebtnRemoveClicked:(id)sender{
self.menuItems = [NSArray arrayWithObjects:#"REMOVE", nil];
NSLog(#"Button remove Clicked");
[self.menuPopover dismissMenuPopover];
self.menuPopover = [[MLKMenuPopover alloc] initWithFrame:MENU_POPOVER_FRAME menuItems:self.menuItems];
self.menuPopover.menuPopoverDelegate = self;
[self.menuPopover showInView:sender];
}

Can't Able to access the Header View in Iphone

I added a header view in the TableView, but I can't able to access that view or any subview of Header view in the IBAction method.
My Code is as follow:
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *header_View=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 160, 40)];
header_View.tag=section;
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.Frame=header_View.bounds;
[btn setTitle:[arr_headerTitles objectAtIndex:section] forState:UIControlStateNormal];
btn.titleLabel.font=FontChampagneLimousinesBold(18);
btn.tag=section;
[btn addTarget:self action:#selector(expandTableHeaderView:) forControlEvents:UIControlEventTouchUpInside];
UIImageView *img_arrow=[[UIImageView alloc]initWithFrame:CGRectMake(150, 05, 15, 15)];
img_arrow.image= [UIImage imageNamed:#"arrow_white_down.png"];
[header_View addSubview:btn];
[header_View addSubview:img_arrow];
return header_View;
}
-(void)expandTableHeaderView:(UIButton*)sender{
// UIView* _header_view =(UIView*)[_tbl viewWithTag:[sender tag]]; //Also tried
UIView *_header_view=[_tbl headerViewForSection:section]; //shows nil;
UIView *view = (UIView *)[_header_view viewWithTag:[sender tag]];
}
Help me to solve this
*Thank you*
Try using UITableViewHeaderFooterView instead of UIView while creating the header_View, seems working
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UITableViewHeaderFooterView *header_View=[[UITableViewHeaderFooterView alloc]initWithFrame:CGRectMake(0, 0, 160, 40)];
header_View.tag=section;
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.Frame=header_View.bounds;
[btn setTitle:[arr_headerTitles objectAtIndex:section] forState:UIControlStateNormal];
btn.titleLabel.font=FontChampagneLimousinesBold(18);
btn.tag=section;
[btn addTarget:self action:#selector(expandTableHeaderView:) forControlEvents:UIControlEventTouchUpInside];
UIImageView *img_arrow=[[UIImageView alloc]initWithFrame:CGRectMake(150, 05, 15, 15)];
img_arrow.image= [UIImage imageNamed:#"arrow_white_down.png"];
[header_View addSubview:btn];
[header_View addSubview:img_arrow];
return header_View;
}
-(void)expandTableHeaderView:(UIButton*)sender{
// UIView* _header_view =(UIView*)[_tbl viewWithTag:[sender tag]]; //Also tried
UITableViewHeaderFooterView *_header_view=[_tbl headerViewForSection:sender.tag]; //shows nil;
UIView *view = (UIView *)[_header_view viewWithTag:[sender tag]];
}
try this:
-(void)expandTableHeaderView:(UIButton*)sender{
UIView *_header_view = sender.superview;
// ...
}
I think your header height is not defined.
Try this:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 40.0;//the height of your header view
}
This might work.
And please put breakpoint marker in your IBAction method to see if its working or not.

Not able to identify the subview of TableView in Iphone

I added a header view with a button & a imageview but I can not Identify the Imageview for changed the Image.
My code is as follow:
My HaderView class .h file (HeaderSection.h)
#import <UIKit/UIKit.h>
#interface HeaderViewSection : UIView
#property(nonatomic,retain)UIButton *btn;
#property(nonatomic,retain)UIImageView *arrow_img;
#end
My HaderView class .m file (HeaderSection.m)
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor brownColor];
_arrow_img=[[UIImageView alloc]init];
_arrow_img.frame=CGRectMake(280, 15, 20, 20);
_arrow_img.image=[UIImage imageNamed:#"Arrow_down_section.png"];
_arrow_img.backgroundColor=[UIColor clearColor];
[self addSubview:_arrow_img];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(20, 15, 35, 25);
[btn setTitle:#"R" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor blackColor];
[self addSubview:btn];
}
return self;
}
.h file
#import "HeaderViewSection.h"
#property (nonatomic,retain)HeaderViewSection *header_view;
.m file
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
_header_view = [[HeaderViewSection alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
_header_view.tag = section+5000;
_header_view.btn.tag = section+5000;
[_header_view.btn addTarget:self action:#selector(expandTableHeaderView:) forControlEvents:UIControlEventTouchUpInside];
_header_view.arrow_img.tag=section+2000;
return _header_view;
}
-(void)expandTableHeaderView:(id)sender{
[self.tblView beginUpdates];
NSInteger section = [sender tag];
UIView *view = (UIView *)[_header_view viewWithTag:[sender tag]]; //RETURNS nil
NSArray *arr = [view subviews]; //RETURNS nil
}
I don't know why this happen? Please Help me to solve this.
In this method you are creating new headerView. Now at this time there is no subView in headerView.
You are setting headerView.btn.tag but you did not add button in headerView, same is the case with image.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
_header_view = [[HeaderViewSection alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
_header_view.tag = section+5000;
UIButton *button = [UIButton alloc] init];//set frame also
button.tag = section+5000;
[button addTarget:self action:#selector(expandTableHeaderView:) forControlEvents:UIControlEventTouchUpInside];
[header_view addSubView:button];
//Do same like image view
}
Edied:
As -(void)expandTableHeaderView:(id)sender{ } method will call only when you tap on button. Button is a subView of headerView. You can have header view by calling its superView method like this
UIView *view = (UIView*)[sender superView];
view.frame = // new frame
now view is you headerView, you can change its frame to expand or what ever you want.
You can get its subViews like this
NSArray *array = [view subViews];
for(UIView *v in array){
if([v isKindOfClass:[UIImage class]]){
//v is imageview change image if you want
}

scroll view + segment button +iOS

I have 2 scroll views. I put many buttons onto the first scroll view. When I click one button in the first scroll view, related buttons fill the second scroll view.
I want the buttons in the first scroll view to have the effect as if they are segment control. i.e. when one button is clicked, its image becomes dim and can't be clicked again until other buttons in the first scroll vew are clicked.
How to achieve this? any sample code is appreciated! thanks!
Can't you just keep track of which button was last clicked, and reset that one to be in the active state, and set the one you're now clicking to the inactive state? Just create a property, lastClicked, and do this:
-(IBAction) buttonClicked:(UIButton *)sender {
[self.lastClicked setUserInteractionEnabled:YES];
[self.lastClicked setImage:[UIImage imageNamed:#"enabledImage"] forState:UIControlStateNormal];
[sender setUserInteractionEnabled:NO];
[sender setImage:[UIImage imageNamed:#"disabledImage"] forState:UIControlStateNormal];
self.lastClicked = sender;
}
Try adding a segmented control inside a scroll view.
- (void)viewDidLoad
{
[super viewDidLoad];
journals = [[NSMutableArray alloc]init];
self.tableView.dataSource = self;
self.tableView.delegate = self;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 49, 320, 29)];
self.segmentedControl.frame = CGRectMake(0, 0, 640, 29);
scrollView.contentSize = CGSizeMake(self.segmentedControl.frame.size.width, self.segmentedControl.frame.size.height -1);
scrollView.showsHorizontalScrollIndicator = NO;
self.segmentedControl.selectedSegmentIndex = 0;
[scrollView addSubview:self.segmentedControl];
[self.view addSubview:scrollView];
[self fillJournals];
// Do any additional setup after loading the view, typically from a nib.
}
Super simple, very ugly, but it works. You could optimize this out further by having it check the image and the interaction state of the button, so you aren't setting an image every time this loops through.
-(void) buttonClicked:(UIButton *)sender {
for (UIButton *btn in self.scrollView.subviews) {
if ([sender isKindOfClass:[UIButton class]]) {
if (btn == sender) {
[btn setUserInteractionEnabled:NO];
[btn setImage:[UIImage imageNamed:#"disabledImage"] forState:UIControlStateNormal];
} else {
[btn setUserInteractionEnabled:YES];
[btn setImage:[UIImage imageNamed:#"enabledImage"] forState:UIControlStateNormal];
}
}
}
}

Resources