I am populating a TableView
dynamically with data called from a server. I have 2 main files, one for the new cell and one for handling the population of the cells within the TableView
Now when I assign [cell.button addTarget:self action:@selector(addFriend:) forControlEvents:UIControlEventTouchUpInside];
to my button within the cellForRowAtIndexPath
and I put an output to log the action of the clicking of the button, nothing happens.
Here is what I am doing:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.......
/* Setup button action */
[cell.button addTarget:self action:@selector(addFriend:) forControlEvents:UIControlEventTouchUpInside];
}
- (IBAction)addFriend:(id)sender
{
NSLog(@"Add friend.");
}
Cell Creation:
HomePageTimelineCell *cell = (HomePageTimelineCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[HomePageTimelineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Suggestions?
UPDATE:
Here is my cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%d",i);
self.tableView.separatorColor = [UIColor clearColor];// Get rid of speration border color
static NSString *CellIdentifier = @"homecell";
HomePageTimelineCell *cell = (HomePageTimelineCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[HomePageTimelineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell initTimelineCell];
cell.backgroundColor = [UIColor clearColor];// Set the background color to the cell
//<!-- Set background color
if (indexPath.row % 2) {
cell.contentView.backgroundColor = [self colorWithHexString:@"77d1af"];
//<!-- Set background and text color for even cells
[cell.button setTitleColor:[self colorWithHexString:@"7ddcb8"] forState:UIControlStateNormal];
cell.button.backgroundColor = [self colorWithHexString:@"666666"];
} else {
cell.contentView.backgroundColor = [self colorWithHexString:@"7ddcb8"];
//<!-- Set background and text color for even cells
[cell.button setTitleColor:[self colorWithHexString:@"7ddcb8"] forState:UIControlStateNormal];
cell.button.backgroundColor = [self colorWithHexString:@"ffffff"];
}
//<!-- if the indexRow row = 0, this is the clients username and address
if(indexPath.row == 0)
{
NSArray *get = [[SSKeychain allAccounts] init];
NSString *username = [get[0] objectForKey:@"acct"];
//<!-- Get keyname of the address and than point to that keyname and get data
NSString *KeyName = [[self.dataGrabed_dictionary allKeys] objectAtIndex: indexPath.row + 4];
//<!-- create uppercase strings
NSString *upperCaseAddress = [[self.dataGrabed_dictionary objectForKey:KeyName] uppercaseString];
NSString *upperCaseUsername = [username uppercaseString];
//<!-- Set associated strings
cell.addressLabel.text = upperCaseAddress;
cell.usernameLabel.text = upperCaseUsername;
}
//<!-- if the indexRow row = 1, this is the 2 cell and will show the most upcoming lawn schedule
else if(indexPath.row == 1)
{ //<!-- create uppercase strings
NSString *upperCaseDay = [[self.dataGrabed_dictionary objectForKey:@"upcoming_day"] uppercaseString];
//<!-- create uppercase strings
NSString *upperCaseDate = [[self.dataGrabed_dictionary objectForKey:@"upcoming_date"] uppercaseString];
cell.contentView.backgroundColor = [self colorWithHexString:@"666666"];
cell.button.backgroundColor = [self colorWithHexString:@"7ddcb8"];
cell.button.tag = indexPath.row;
[cell.button setTitle:@"change" forState:UIControlStateNormal];
[cell.button setTitleColor:[self colorWithHexString:@"666666"] forState:UIControlStateNormal];
/* Setup button action */
[cell.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
cell.dayLabel.text = upperCaseDay;//<!-- Set the day
cell.dateLabel.text = upperCaseDate;//<!-- Set the Date
[cell.contentView addSubview:cell.button];
[cell.contentView addSubview:cell.nextLabel];
[cell.contentView addSubview:cell.dayLabel];
[cell.contentView addSubview:cell.dateLabel];
}
else //<!-- Normal cell population
{
//<!-- Re edit labels positioning
cell.dayLabel.frame = CGRectMake(9, 10, 200, 45);
cell.dateLabel.frame = CGRectMake(9, 35, 300, 45);
//<!-- Setup data called from server
NSDictionary *innerClientData =[self.dataGrabed_dictionary objectForKey:@"scheduled_times"][i];
NSString *innerClientDay =[self.dataGrabed_dictionary objectForKey:@"scheduled_times_day"][i];
NSString *innerClientDate =[self.dataGrabed_dictionary objectForKey:@"scheduled_times_date"][i];
//<!-- create uppercase strings
NSString *upperCaseDay = [innerClientDay uppercaseString];
//<!-- create uppercase strings
NSString *upperCaseDate = [ innerClientDate uppercaseString];
//<!-- Check to see if client paid
if([[innerClientData objectForKey:@"client_paid"] isEqual: @"0"])
{
NSString *amount = [innerClientData objectForKey:@"client_price"];
NSString *pay = @"pay $";
NSString * combined = [pay stringByAppendingString:amount];
[cell.button setTitle:combined forState:UIControlStateNormal];
}
else if([[innerClientData objectForKey:@"client_paid"] isEqual: @"1"])
{
[cell.button setTitle:@"PAID" forState:UIControlStateNormal];
}
//[cell.button setTitleColor:[self colorWithHexString:@"666666"] forState:UIControlStateNormal];
cell.button.tag = indexPath.row;
/* Setup button action */
[cell.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
cell.dayLabel.text = upperCaseDay;//<!-- Set the day
cell.dateLabel.text = upperCaseDate;//<!-- Set the Date
[cell.contentView addSubview:cell.button];
[cell.contentView addSubview:cell.dayLabel];
[cell.contentView addSubview:cell.dateLabel];
i++;
}
return cell;
}
Here is my HomePageTimelineCell
cell creator:
#import "HomePageTimelineCell.h"
#import "HomePage.h"
@implementation HomePageTimelineCell
@synthesize cellContentView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
cellContentView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 300, 85)];
cellContentView.backgroundColor = [UIColor clearColor];
_usernameLabel = [[UILabel alloc] initWithFrame:CGRectMake(9, 10, 300, 45)];
_usernameLabel.font = [UIFont fontWithName:@"AppleSDGothicNeo-SemiBold" size:30.0];
_usernameLabel.textColor = [UIColor whiteColor];
_addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(9, 40, 300, 45)];
_addressLabel.font = [UIFont fontWithName:@"AppleSDGothicNeo-Light" size:20.0];
_addressLabel.textColor = [UIColor whiteColor];
_addressLabel.numberOfLines = 1;
_addressLabel.lineBreakMode = NSLineBreakByTruncatingTail;
_nextLabel = [[UILabel alloc] initWithFrame:CGRectMake(9, 0, 100, 45)];
_nextLabel.font = [UIFont fontWithName:@"AppleSDGothicNeo-Thin" size:25.0];
_nextLabel.textColor = [UIColor whiteColor];
_nextLabel.text = @"NEXT";
_dayLabel = [[UILabel alloc] initWithFrame:CGRectMake(9, 25, 200, 45)];
_dayLabel.font = [UIFont fontWithName:@"AppleSDGothicNeo-SemiBold" size:35.0];
_dayLabel.textColor = [UIColor whiteColor];
_dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(9, 50, 300, 45)];
_dateLabel.font = [UIFont fontWithName:@"AppleSDGothicNeo-Thin" size:25.0];
_dateLabel.textColor = [UIColor whiteColor];
// IMPACT BUTTON STYLES
_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_button.layer.cornerRadius = 0.0f;
_button.frame = CGRectMake(220, 20, 80, 45);
/* ADD ALL THE CONTENT */
[self addSubview:cellContentView];
[cellContentView addSubview:_usernameLabel];
[cellContentView addSubview:_addressLabel];
}
return self;
}
- (void) initTimelineCell
{
cellContentView.layer.cornerRadius = 2;
cellContentView.layer.masksToBounds = NO;
configured = YES;
}
- (bool) configured
{
return configured;
}
@end
Premise that you should provide more code to have a clear idea of what is happening, this is not the best approach.
At least I hope you are adding the target just if the cell is nil
and you are init that and not more and more time :)
But anyway, the correct approach in functionality and design , is to implement the button action in the cell subclass , and return back by a delegate the cell where the button touched was.
I explained the various approaches here , the last one is this.
Enjoy clean code and good design ;)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.