there is this dark sorcery in ios is preventing my button being clicked. if i don't add button to the uitableviewcell, and i click the button, the event is triggered.
but if the button is in uitableviewcell, it won't get triggered, it seems table
i have sample code ready, if you guys can help me, please just create a simple single-view application in xcode, and just paste the following code
//GRDViewController.h
#import <UIKit/UIKit.h>
#interface GRDViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) UIView* container;
#property (nonatomic, strong) UIButton* button;
#property (nonatomic, strong) UITableView* tableView;
#property (nonatomic, strong) NSArray* arr;
#end
//GRDViewController.m
#import "GRDViewController.h"
#implementation GRDViewController
#synthesize button, container, arr, tableView;
- (void)_btnTapped {
NSLog(#"TAPPED");
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
[self.button addTarget:self action:#selector(_btnTapped) forControlEvents:UIControlEventTouchUpInside];
[self.button setTitle:#"CLICK ME" forState:UIControlStateNormal];
self.arr = [[NSArray alloc] initWithObjects:#"123", #"456", #"678", nil];
self.container = [[UIView alloc]initWithFrame:self.button.frame];
[self.container addSubview:self.button];
[self.view addSubview:self.container];
//[self.view addSubview:self.button];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 100, 400, 400)];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
self.tableView.backgroundColor = [UIColor redColor];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"coolcell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//cell.accessoryType = UITableViewCellAccessoryNone;
UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
[btn addTarget:self action:#selector(_btnTapped) forControlEvents:UIControlStateNormal];
[btn setTitle:[self.arr objectAtIndex:[indexPath row]] forState:UIControlStateNormal];
[cell.contentView addSubview:btn];
}
return cell;
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
return [self.arr count];
}
#end
please help
i feel this case should be common in ios, but nobody has solution for this???
edit: when click on the button it prints NSLOG msg in the xcode console...so make sure...u r looking at the results there...
you have not given any control event to button .Change it to UIControlEventTouchUpInside from UIControlStateNormal
Instead of using UIControlStateNormal for ControlEvents use UIControlEventTouchUpInside like this
[btn addTarget:self action:#selector(_btnTapped) forControlEvents:UIControlEventTouchUpInside];
Try doing this:
[btn.titleLabel setUserInteractionEnabled: NO];
Related
Hi I am very new to iOS and in my project iI have created one UITableView with images as like my below screen ok that's fine
And here when I tapped on tableView images I want show that related row images with popup block using one UIView.
But using my code image is not showing on popup UIView. Please see my below screen image is not adding on UIView popup block please help me some one.
My Code:
#import "imageTableViewController.h"
#interface imageTableViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *mainTable;
NSMutableArray *imageArray;
UIButton *imageButton;
UIView *popUpView;
BOOL isFullScreen;
CGRect prevFrame;
}
#end
#implementation imageTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
imageArray = [[NSMutableArray alloc] initWithObjects:#"flower.jpeg",#"Bird.jpg",#"Browser.jpeg", nil];
mainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)];
mainTable.dataSource = self;
mainTable.delegate = self;
[self.view addSubview:mainTable];
popUpView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 25, 25)];
popUpView.backgroundColor = [UIColor orangeColor];
popUpView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
[mainTable addSubview:popUpView];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
imageView.image = [UIImage imageNamed:#"flower.jpeg"];
[popUpView addSubview:imageView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return imageArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (Cell == nil)
{
Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
imageButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
[imageButton setImage:[UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]] forState:UIControlStateNormal];
[imageButton addTarget:self action:#selector(imgToFullScreen:) forControlEvents:UIControlEventTouchUpInside];
[Cell.contentView addSubview:imageButton];
return Cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
-(void)imgToFullScreen:(UITapGestureRecognizer*)sender {
if (!isFullScreen) {
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
popUpView.hidden = NO;
prevFrame = CGRectMake(30, 30, 25, 25);
[popUpView setFrame:CGRectMake(120, 50, 150, 150)];
}completion:^(BOOL finished){
isFullScreen = TRUE;
}];
return;
}
else{
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
[popUpView setFrame:prevFrame];
}completion:^(BOOL finished){
isFullScreen = FALSE;
popUpView.hidden = YES;
}];
return;
}
}
Looks like it's happened because of
popUpView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
you create view and scale it to really small, also all subview will be downscaled. And then in show animation block you change frame of popup, but you have to change scale like:
popUpView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
Edit due comment
hmm yes that's fine and one small problem is coming #in.disee that is
when i zoom in popup block images are popup like my above screen and
when i zoom out that images are must zoom out near related
rows,understand?
Your current realisation have few problems including:
Your architecture do not allow you get index path of selected cell - it will be a problem anyway, so among other you have to make this part.
It will be too hard explain in words everything you have to chage, so i write code for you) I almost do not change your code, but it would be cool if you change it to the way i write mine, because it's more convenient to apple guides
What i do:
1) create custom class for cell
2) add ability for cell say something to tableview via delegation pattern
3) when user click on button in cell - cell tells to tableview, which button of which was was pressed
4) table view convert frame of cell's image to own coordinates and show popup
you can just replace you code with this one:
#import "imageTableViewController.h"
#class CustomCell;
#protocol CustomCellDelegate <NSObject>
- (void)didSelectImageNamed:(NSString *)name fromCell:(CustomCell *)cell;
#end
#interface CustomCell : UITableViewCell
#property (nonatomic, strong) NSString *imageName;
#property (nonatomic, strong) UIButton *imageBtn;
#property (nonatomic, weak) id <CustomCellDelegate> delegate;
#property (nonatomic, assign, readonly) CGRect imageRect;
#end
#implementation CustomCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self){
[self setup];
}
return self;
}
- (CGRect)imageRect {
return self.imageBtn.frame;
}
- (void)setup {
self.imageBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
[self.imageBtn addTarget:self action:#selector(imgToFullScreen:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.imageBtn];
}
- (void)prepareForReuse {
[self.imageBtn setImage:nil forState:UIControlStateNormal];
}
- (void)setImageName:(NSString *)imageName {
_imageName = imageName;
[self.imageBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
}
- (void)imgToFullScreen:(UIButton *)sender {//btw it's not UITapGestureRecognizer *
[self.delegate didSelectImageNamed:self.imageName fromCell:self];
}
#end
#interface imageTableViewController ()
<CustomCellDelegate>
#end
#implementation imageTableViewController {
UITableView *mainTable;
NSMutableArray *imageArray;
UIButton *imageButton;
UIView *popUpView;
BOOL isFullScreen;
CGRect prevFrame;
}
- (void)viewDidLoad {
[super viewDidLoad];
imageArray = [[NSMutableArray alloc] initWithObjects:#"flower.jpeg",#"Bird.jpg",#"Browser.jpeg", nil];
mainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)];
mainTable.dataSource = self;
mainTable.delegate = self;
[self.view addSubview:mainTable];
popUpView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 25, 25)];
popUpView.backgroundColor = [UIColor orangeColor];
popUpView.hidden = YES;
[mainTable addSubview:popUpView];
UIView * imageView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
imageView.backgroundColor = [UIColor colorWithRed:255./255. green:0 blue:0 alpha:1];
[popUpView addSubview:imageView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return imageArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
CustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (Cell == nil)
{
Cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Cell.delegate = self;
}
Cell.imageName = [imageArray objectAtIndex:indexPath.row];
return Cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
-(void)imgToFullScreen:(NSString *)imageName fromRect:(CGRect)frame {
if (!isFullScreen) {
[popUpView setFrame:frame];
[popUpView setHidden:NO];
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
[popUpView setFrame:CGRectMake(120, 50, 150, 150)];
}completion:^(BOOL finished){
isFullScreen = YES;
prevFrame = frame;
}];
return;
}
else{
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
[popUpView setFrame:prevFrame];
}completion:^(BOOL finished){
isFullScreen = NO;
popUpView.hidden = YES;
}];
return;
}
}
#pragma mark - CustomCellDelegate
- (void)didSelectImageNamed:(NSString *)name fromCell:(CustomCell *)cell {
CGRect imageFrame = cell.imageRect;
CGRect imageFrameGlobalCoord = [mainTable convertRect:imageFrame fromView:cell];
[self imgToFullScreen:name fromRect:imageFrameGlobalCoord];
}
#end
I currently have a UITableView within my MatchCenterViewController, and I designed it to load up 10 rows for every section, but only show the first 4 by making the heightForRowAtIndexPath return a value of 0 for the rest. What I want to do is have a button on the bottom of every section that when pressed, will reload the data and show 10 instead of 4 for just that specific section.
I've started working on the framework for what happens when the button is pressed, I'm just having trouble with the syntax for rendering the button on the bottom and showing 10 rows for only that respective section. Here's how I have my UITableView laid out so far:
MatchCenterViewController.h:
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "AsyncImageView.h"
#import "SearchViewController.h"
#import "WebViewController.h"
#import "SLExpandableTableView.h"
#interface MatchCenterViewController : UIViewController <UITableViewDataSource>
#property (strong, nonatomic) NSString *itemSearch;
#property (nonatomic, strong) NSArray *imageURLs;
#property (strong, nonatomic) NSString *matchingCategoryCondition;
#property (strong, nonatomic) NSString *matchingCategoryLocation;
#property (strong, nonatomic) NSNumber *matchingCategoryMaxPrice;
#property (strong, nonatomic) NSNumber *matchingCategoryMinPrice;
#property (strong, nonatomic) NSArray *matchCenterArray;
#property (strong, nonatomic) NSString *searchTerm;
#property (strong, nonatomic) NSString *itemURL;
#end
MatchCenterViewController.m:
#import "MatchCenterViewController.h"
#import <UIKit/UIKit.h>
#interface MatchCenterViewController () <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) UITableView *matchCenter;
#property (nonatomic, assign) BOOL matchCenterDone;
#property (nonatomic, assign) BOOL hasPressedShowMoreButton;
#end
#implementation MatchCenterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_matchCenterDone = NO;
//self.matchCenter = [[SLExpandableTableView alloc] initWithFrame:self.view.bounds style:UITableViewCellStyleSubtitle];
self.matchCenter = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewCellStyleSubtitle];
self.matchCenter.frame = CGRectMake(0,50,320,self.view.frame.size.height-100);
_matchCenter.dataSource = self;
_matchCenter.delegate = self;
[self.view addSubview:self.matchCenter];
_matchCenterArray = [[NSArray alloc] init];
}
- (void)viewDidAppear:(BOOL)animated
{
self.matchCenterArray = [[NSArray alloc] init];
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);
[self.view addSubview: activityIndicator];
[activityIndicator startAnimating];
_matchCenterDone = NO;
// Disable ability to scroll until table is MatchCenter table is done loading
self.matchCenter.scrollEnabled = NO;
[PFCloud callFunctionInBackground:#"MatchCenter2"
withParameters:#{}
block:^(NSArray *result, NSError *error) {
if (!error) {
_matchCenterArray = result;
[activityIndicator stopAnimating];
[_matchCenter reloadData];
_matchCenterDone = YES;
self.matchCenter.scrollEnabled = YES;
NSLog(#"Result: '%#'", result);
}
}];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _matchCenterArray.count;
}
//the part where i setup sections and the deleting of said sections
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 21.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 21)];
headerView.backgroundColor = [UIColor lightGrayColor];
_searchTerm = [[[[_matchCenterArray objectAtIndex:section] objectForKey:#"Top 3"] objectAtIndex:0]objectForKey:#"Search Term"];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 0, 250, 21)];
headerLabel.text = [NSString stringWithFormat:#"%#", _searchTerm];
headerLabel.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
headerLabel.textColor = [UIColor whiteColor];
headerLabel.backgroundColor = [UIColor lightGrayColor];
[headerView addSubview:headerLabel];
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButton.tag = section;
deleteButton.frame = CGRectMake(300, 2, 17, 17);
[deleteButton setImage:[UIImage imageNamed:#"xbutton.png"] forState:UIControlStateNormal];
[deleteButton addTarget:self action:#selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:deleteButton];
return headerView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary *currentSectionDictionary = _matchCenterArray[section];
NSArray *top3ArrayForSection = currentSectionDictionary[#"Top 3"];
return top3ArrayForSection.count-1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Initialize cell
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// No cell separators = clean design
tableView.separatorColor = [UIColor clearColor];
// title of the item
cell.textLabel.text = _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Title"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
// price of the item
cell.detailTextLabel.text = [NSString stringWithFormat:#"$%#", _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Price"]];
cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];
// image of the item
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Image URL"]]];
[[cell imageView] setImage:[UIImage imageWithData:imageData]];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row > 3 || self.hasPressedShowMoreButton){
return 0;
}
else{
return 65;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_matchCenterDone == YES) {
self.itemURL = _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row][#"Item URL"];
[self performSegueWithIdentifier:#"WebViewSegue" sender:self];
}
}
-(IBAction)pressedShowMoreButton{
self.hasPressedShowMoreButton = YES;
[self.matchCenter reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
WebViewController *controller = (WebViewController *) segue.destinationViewController;
controller.itemURL = self.itemURL;
}
#end
for this functionality you can either use special UITableviewCell or a footerView of UITableView
You can use property tableFooterView.
Below is how I use in showing load more option
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
v.backgroundColor = [UIColor clearColor];
int mySiz = 0;
// keep counter how many times load more is pressed.. initial is 0 (this is like index)
mySiz = [startNumberLabel.text intValue]+1;
// i have 15 bcz my index size is 15.
if ([feeds count]>=(15*mySiz)) {
NSLog(#"showing button...");
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(10, 10, 296, 45)];
[button setBackgroundImage:[UIImage imageNamed:localize(#"loadmore")] forState:UIControlStateNormal];
[button addTarget:self action:#selector(loadMoreData:) forControlEvents:UIControlEventTouchUpInside];
[v addSubview:button];
mainTableView.tableFooterView = v;
} else {
mainTableView.tableFooterView = nil;
}
[mainTableView reloadData];
Now adjust code as per your necessity...
Ended up doing it like this:
// Create "more" button
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
view.backgroundColor = [UIColor whiteColor];
self.moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.moreButton.frame = CGRectMake(0, 0, 320, 44);
[self.moreButton setImage:[UIImage imageNamed:#"downarrow.png"] forState:UIControlStateNormal];
[self.moreButton addTarget:self action:#selector(moreButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:self.moreButton];
return view;
}
// Load rest of items
- (void)moreButtonSelected:(id)sender {
if (_hasPressedShowMoreButton == NO){
self.hasPressedShowMoreButton = YES;
}
else if (_hasPressedShowMoreButton == YES){
self.hasPressedShowMoreButton = NO;
}
[self.matchCenter reloadData];
}
I am not a full-time iOS dev, so this may be simple. I have the following in a custom UITableViewCell but it doesn't seem to be working. The vast majority have the selector being in the Controller. Is this possible? What am I doing wrong here?
In my custom UITableViewCell
-(void)updateCell:(NSDictionary *)content
{
UILabel *mainLabel=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 200.0f, 20.0f)];
mainLabel.text=[content objectForKey:#"name"];
[self.contentView addSubview:mainLabel];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f, 20.0f, 50.0f, 20.0f);
[self.contentView addSubview:button];
}
-(void)aMethod:(id)sender
{
NSLog(#"here is a method");
}
which is called in my ViewController in cellForRowAtIndexPath.
Edit
Basically I would like to get an expanded content effect similar to this: http://jsfiddle.net/bso3sa07/
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
ViewController.m
#import "ViewController.h"
#import "ContentCell.h"
#interface ViewController (){
NSMutableArray *_data;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.dataSource=self;
self.tableView.delegate=self;
NSDictionary *obj1=#{#"name":#"Julie"};
NSDictionary *obj2=#{#"name":#"Melissa"};
_data=[[NSMutableArray alloc] init];
[_data addObject:obj1];
[_data addObject:obj2];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_data count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
id tmpCell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
ContentCell *cell=(ContentCell *)tmpCell;
return cell.height;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"ContentCell";
ContentCell *cell = (ContentCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[ContentCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
[cell updateCell:[_data objectAtIndex:indexPath.row]];
UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[moreButton addTarget:self
action:#selector(vcSelector)
forControlEvents:UIControlEventTouchUpInside];
[moreButton setTitle:#"vcMore" forState:UIControlStateNormal];
moreButton.frame = CGRectMake(200.0f, 10.0f, 100.0f, 20.0f);
[cell.contentView addSubview:moreButton];
return cell;
}
-(void)vcSelector
{
NSLog(#"hello from vcSelector");
// lets get a reference to that specific cell
// how would I expand that specific cell?
/*
id tmpCell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
ContentCell *cell=(ContentCell *)tmpCell;
return cell.height;
*/
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
ContentCell.h
#import <UIKit/UIKit.h>
#interface ContentCell : UITableViewCell
#property (nonatomic, assign) CGFloat height;
-(void)updateCell:(NSDictionary *)obj;
#end
ContentCell.m
#import "ContentCell.h"
#implementation ContentCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)awakeFromNib
{
// Initialization code
}
-(void)updateCell:(NSDictionary *)content
{
self.height=60.0f;
UILabel *mainLabel=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0f, 20.0f)];
mainLabel.text=[content objectForKey:#"name"];
mainLabel.layer.borderColor=[UIColor greenColor].CGColor;
mainLabel.layer.borderWidth=2.0f;
[self.contentView addSubview:mainLabel];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f, 20.0f, 100.0f, 20.0f);
[self.contentView addSubview:button];
}
-(void)aMethod:(id)sender
{
NSLog(#"here is a method");
UIView *pushView=[[UIView alloc] initWithFrame:CGRectMake(10.0f, 50.0f, 300.0f, 5.0f)];
[pushView setBackgroundColor:[UIColor redColor]];
[self.contentView addSubview:pushView];
self.height=90.0f;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
Try something like this:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
// Check if button exist already
UIButton *button = (UIButton*)[cell viewWithTag:555];
// if not - create new one
if (!button)
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag = 555;
button.frame = CGRectMake(0.0f, 20.0f, 50.0f, 20.0f);
[cell addSubview:button];
}
// update title and selector if required
[button addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
return cell;
}
I have a UIButton subclass which customizes a button. Then I have a UITableViewCell subclass which uses this button and position it in the cell. Finally I have a subclass of UITableViewController which displays the table view and contains the customized cell. My problem is that the customized button "starbtn" won't show up in the table, while everything else is working properly. What is wrong in my code?
favButton.h
#import <UIKit/UIKit.h>
#interface favButton : UIButton {
BOOL _checked;
}
#property (nonatomic, setter = setChecked:) BOOL checked;
-(void) setChecked:(BOOL) check;
#end
favButton.m
#import "favButton.h"
#implementation favButton
#synthesize checked = _checked;
-(id) init
{
if( self=[super init] )
{
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void) awakeFromNib
{
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
-(void) setChecked:(BOOL) check
{
_checked = check;
if( _checked )
{
[self setTintColor:[UIColor clearColor]];
UIImage* img = [UIImage imageNamed:#"startbefore.jpg"];
[self setImage:img forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected|UIControlStateDisabled];
}
else
{
[self setTintColor:[UIColor clearColor]];
UIImage* img = [UIImage imageNamed:#"startafter.jpg"];
[self setImage:img forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected|UIControlStateDisabled];
}
}
-(void) OnCheck:(id) sender
{
self.checked = !_checked;
}
#end
imageCellCell.h
#import <UIKit/UIKit.h>
#import "favButton.h"
#interface imageCellCell : UITableViewCell
#property (nonatomic, strong) UIView *view;
#property (nonatomic, strong) UILabel *label1;
#property (nonatomic, strong) UILabel *label2;
#property (nonatomic, strong) UIImageView *prodimage;
#property (nonatomic, strong) UIImageView *thumbsup;
#property (nonatomic, strong) UILabel *label3;
#property (nonatomic, strong) UIImageView *basket;
#property (nonatomic, strong) UIButton *homebtn;
#property (nonatomic, strong) favButton *starbtn;
#end
imageCellCell.m
#import "imageCellCell.h"
#implementation imageCellCell
#synthesize view;
#synthesize label1;
#synthesize label2;
#synthesize prodimage;
#synthesize thumbsup;
#synthesize label3;
#synthesize basket;
#synthesize homebtn;
#synthesize starbtn;
- (instancetype)init {
self = [super init];
self.starbtn = [[favButton alloc]initWithFrame:CGRectMake(243,0, 30, 30)];
[starbtn setTintColor:[UIColor clearColor]];
[starbtn setBackgroundImage:[UIImage imageNamed:#"startbefore.jpg"]
forState:UIControlStateNormal];
return self;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
view = [[UIView alloc] initWithFrame:self.frame];
[self addSubview:view];
// initiate image
prodimage = [[UIImageView alloc]initWithFrame:CGRectMake(30,2, 180, 180)];
// initiate label1 score value
label1 = [[UILabel alloc] initWithFrame:CGRectMake(230,80,150,20)];
label1.textColor = [UIColor greenColor];
[[self label1] setFont:[UIFont systemFontOfSize:25]];
// initiate label2 score label
label2 = [[UILabel alloc] initWithFrame:CGRectMake(232,50,150,20)];
// initiate image
thumbsup = [[UIImageView alloc]initWithFrame:CGRectMake(240,108, 30, 30)];
// initiate label3 manufacturer
label3 = [[UILabel alloc] initWithFrame:CGRectMake(90,200,180,20)];
// initiate image
basket = [[UIImageView alloc]initWithFrame:CGRectMake(280,1, 30, 30)];
// initiate home button
homebtn = [[UIButton alloc]initWithFrame:CGRectMake(4,0, 30, 30)];
[homebtn setTintColor:[UIColor clearColor]];
[homebtn setBackgroundImage:[UIImage imageNamed:#"home.jpg"]
forState:UIControlStateNormal];
[view addSubview:prodimage];
[view addSubview:label1];
[view addSubview:label2];
[view addSubview:thumbsup];
[view addSubview:label3];
[view addSubview:basket];
[view addSubview:homebtn];
[view addSubview:starbtn];
}
return self;
}
#end
ProdViewController.m
#import "ProdViewController.h"
#import "imageCellCell.h"
#interface ProdViewController ()
#end
#implementation ProdViewController
#synthesize tableview;
#synthesize tabbar;
#synthesize addfav;
#synthesize favData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
favData = [[NSMutableArray alloc] init];
[tableview setDataSource:self];
[tableview setDelegate:self];
}
- (void)viewDidUnload
{
[self setTableview:nil];
[self setTabbar:nil];
[self setAddfav:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier;
NSString *CellIdentifierimg;
UITableViewCell *cell;
if (cell == nil) {
if (indexPath.row == 0) {
cell = [[imageCellCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierimg];
} else if (indexPath.row == 1) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}else if (indexPath.row == 2) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}else if (indexPath.row == 3) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}else if (indexPath.row == 4) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
}
switch ([indexPath row])
{
case 0:
{
imageCellCell *firstRowCell = (imageCellCell *)cell;
[firstRowCell.thumbsup setImage: [UIImage imageNamed:#"thumbs-up-green.jpg"]];
[firstRowCell.prodimage setImage: [UIImage imageNamed:#"test1.jpg"]];
[firstRowCell.label1 setText:#"10"];
[firstRowCell.label2 setText:#"Score"];
[firstRowCell.label3 setText:#"Name"];
[firstRowCell.basket setImage: [UIImage imageNamed:#"Basket.jpg"]];
// reference of the home button to the buttonclick method
[firstRowCell.homebtn addTarget:self action:#selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
// reference of the favorites button to the buttonclick method
[firstRowCell.starbtn addTarget:self action:#selector(clickFavButton:) forControlEvents:UIControlEventTouchUpInside];
firstRowCell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
}
case 1:
{
cell.textLabel.text = #"Row 2";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
}
case 2:
{
cell.textLabel.text = #"Row 3";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
}
case 3:
{
cell.textLabel.text = #"Row 4";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
}
}
return cell;
}
-(IBAction)clickFavButton:(id)sender{
[favData addObject:#"productname"];
}
#end
why u are adding the starbtn in init method, put that code in - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
comment out entire init method
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
view = [[UIView alloc] initWithFrame:self.frame];
[self addSubview:view];
// initiate image
prodimage = [[UIImageView alloc]initWithFrame:CGRectMake(30,2, 180, 180)];
// initiate label1 score value
label1 = [[UILabel alloc] initWithFrame:CGRectMake(230,80,150,20)];
label1.textColor = [UIColor greenColor];
[[self label1] setFont:[UIFont systemFontOfSize:25]];
// initiate label2 score label
label2 = [[UILabel alloc] initWithFrame:CGRectMake(232,50,150,20)];
// initiate image
thumbsup = [[UIImageView alloc]initWithFrame:CGRectMake(240,108, 30, 30)];
// initiate label3 manufacturer
label3 = [[UILabel alloc] initWithFrame:CGRectMake(90,200,180,20)];
// initiate image
basket = [[UIImageView alloc]initWithFrame:CGRectMake(280,1, 30, 30)];
// initiate home button
homebtn = [[UIButton alloc]initWithFrame:CGRectMake(4,0, 30, 30)];
[homebtn setTintColor:[UIColor clearColor]];
[homebtn setBackgroundImage:[UIImage imageNamed:#"home.jpg"]
forState:UIControlStateNormal];
//initilise hear
//edit
starbtn = [[favButton alloc]init];
starbtn.frame = CGRectMake(243,0, 30, 30)];
//edit
[starbtn setTintColor:[UIColor clearColor]];
[starbtn setBackgroundImage:[UIImage imageNamed:#"startbefore.jpg"]
forState:UIControlStateNormal];
[view addSubview: starbtn]; //add this
[view addSubview:prodimage];
[view addSubview:label1];
[view addSubview:label2];
[view addSubview:thumbsup];
[view addSubview:label3];
[view addSubview:basket];
[view addSubview:homebtn];
[view addSubview:starbtn];
}
return self;
}
Edited Answer
in your "favButton.h" file
#import <UIKit/UIKit.h>
#interface favButton : UIButton
{
BOOL _checked;
}
#property (nonatomic, setter = setChecked:) BOOL checked;
-(void) setChecked:(BOOL) check;
#end
in "favButton.m" file
#import "favButton.h"
#implementation favButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void) awakeFromNib
{
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
-(void) setChecked:(BOOL) check
{
_checked = check;
if( _checked )
{
// [self setTintColor:[UIColor clearColor]];
UIImage* img = [UIImage imageNamed:#"22.jpg"];
// [self setImage:img forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected|UIControlStateDisabled];
[self setImage:[UIImage imageNamed:#"22.jpg"] forState:UIControlStateNormal];
}
else
{
// [self setTintColor:[UIColor clearColor]];
UIImage* img = [UIImage imageNamed:#"33.jpg"];
// [self setImage:img forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected|UIControlStateDisabled];
[self setImage:[UIImage imageNamed:#"33.jpg"] forState:UIControlStateNormal];
}
}
-(void) OnCheck:(id) sender
{
self.checked = !_checked;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
in imageCellCell.h file
include only these methods
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
// Initialization code
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
view = [[UIView alloc] initWithFrame:self.frame];
[self addSubview:view];
// initiate image
prodimage = [[UIImageView alloc]initWithFrame:CGRectMake(30,2, 180, 180)];
// initiate label1 score value
label1 = [[UILabel alloc] initWithFrame:CGRectMake(230,80,150,20)];
label1.textColor = [UIColor greenColor];
[[self label1] setFont:[UIFont systemFontOfSize:25]];
// initiate label2 score label
label2 = [[UILabel alloc] initWithFrame:CGRectMake(232,50,150,20)];
// initiate image
thumbsup = [[UIImageView alloc]initWithFrame:CGRectMake(240,108, 30, 30)];
// initiate label3 manufacturer
label3 = [[UILabel alloc] initWithFrame:CGRectMake(90,200,180,20)];
// initiate image
basket = [[UIImageView alloc]initWithFrame:CGRectMake(280,1, 30, 30)];
// initiate home button
homebtn = [[UIButton alloc]initWithFrame:CGRectMake(4,0, 30, 30)];
[homebtn setTintColor:[UIColor clearColor]];
[homebtn setBackgroundImage:[UIImage imageNamed:#"home.jpg"]
forState:UIControlStateNormal];
starbtn = [[favButton alloc]initWithFrame:CGRectMake(243,0, 30, 30)];
starbtn.backgroundColor = [UIColor greenColor];
// starbtn.frame = CGRectMake(243,0, 30, 30);
//edit
[starbtn setTintColor:[UIColor clearColor]];
[starbtn setBackgroundImage:[UIImage imageNamed:#"startbefore.jpg"]
forState:UIControlStateNormal];
[view addSubview: starbtn]; //add this
[view addSubview:prodimage];
[view addSubview:label1];
[view addSubview:label2];
[view addSubview:thumbsup];
[view addSubview:label3];
[view addSubview:basket];
[view addSubview:homebtn];
[view addSubview:starbtn];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
in controller all are same no need to change
End Edit
Hope this helps u .. :)
I am aware that this is not a really advanced question, but I feel lost. I have a class that defines a custom table cell style, which contains the desired button I wish to edit. Then in a different class I use this cell type and construct a table view. Then I am writing an IBAction to the button of the different class. When the button is tapped, it should remain selected. I have tried several things but I can't get a reference of the button properly to provide the desired outcome.
I am still trying to adapt to Objective-C.
customcellstyle.m
#import "imageCellCell.h"
#implementation imageCellCell
#synthesize view;
#synthesize starbtn;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
view = [[UIView alloc] initWithFrame:self.frame];
[self addSubview:view];
// initiate favorites button
starbtn = [[UIButton alloc]initWithFrame:CGRectMake(243,0, 30, 30)];
[starbtn setTintColor:[UIColor clearColor]];
[starbtn setBackgroundImage:[UIImage imageNamed:#"startbefore.jpg"]
forState:UIControlStateNormal];
[starbtn setBackgroundImage:[UIImage imageNamed:#"startafter.jpg"]
forState:UIControlStateSelected];
[starbtn setBackgroundImage:[UIImage imageNamed:#"startafter.jpg"]
forState:UIControlStateHighlighted];
[starbtn setBackgroundImage:[UIImage imageNamed:#"startafter.jpg"]
[view addSubview:starbtn];
forState:UIControlStateHighlighted|UIControlStateSelected|UIControlStateDisabled];
tableclass.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier;
NSString *CellIdentifierimg;
UITableViewCell *cell;
if (cell == nil) {
if (indexPath.row == 0) {
cell = [[imageCellCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierimg];
} else if (indexPath.row == 1) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
}
switch ([indexPath row])
{
case 0:
{
imageCellCell *firstRowCell = (imageCellCell *)cell;
// reference of the favorites button to the buttonclick method
[firstRowCell.starbtn addTarget:self action:#selector(clickFavButton:) forControlEvents:UIControlEventTouchUpInside];
firstRowCell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
}
-(IBAction)clickFavButton:(id)sender{
customcellstyle *class = [[customcellstyle alloc] init];;
[class.starbtn setSelected:YES];
[class.starbtn setHighlighted:YES];
[starbtn setSelected:YES];
[starbtn setHighlighted:YES];
}
You should create a custom button class where you need to set and handle the button image using it's selected value, i have done same kinda thing for checkbox button like
in .h file
#interface MXCheckButton : UIButton {
BOOL _checked;
}
#property (nonatomic, setter = setChecked:) BOOL checked;
-(void) setChecked:(BOOL) check;
#end
and in your .m file
#implementation MXCheckButton
#synthesize checked = _checked;
-(id) init
{
if( self=[super init] )
{
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void) awakeFromNib
{
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
-(void) setChecked:(BOOL) check
{
_checked = check;
if( _checked )
{
UIImage* img = [UIImage imageNamed:#"checked.png"];
[self setImage:img forState:UIControlStateNormal];
}
else
{
UIImage* img = [UIImage imageNamed:#"unchecked.png"];
[self setImage:img forState:UIControlStateNormal];
}
}
-(void) OnCheck:(id) sender
{
self.checked = !_checked;
}
#end
here you just change the image file name and your are ready to go!