在TableView单元格创建中向UIButton分配操作

时间:2015-01-13 22:20:21

标签: ios objective-c uitableview uibutton

我使用从服务器调用的数据动态填充TableView。我有2个主要文件,一个用于新单元格,另一个用于处理TableView中单元格的填充现在当我将[cell.button addTarget:self action:@selector(addFriend:) forControlEvents:UIControlEventTouchUpInside];分配给cellForRowAtIndexPath中的按钮时,我放了一个输出以记录单击按钮的操作,没有任何反应。

这是我正在做的事情:

- (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.");
}

细胞创建:

HomePageTimelineCell *cell = (HomePageTimelineCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[HomePageTimelineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

建议?

更新:

这是我的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;
}

这是我的HomePageTimelineCell单元格创建者:

#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

1 个答案:

答案 0 :(得分:2)

前提是您应该提供更多代码以清楚地了解发生的情况, 这不是最好的方法。

至少我希望你正在添加目标,只要单元格是nil并且你是初始化而不是越来越多的时间:)

但无论如何,正确的功能和设计方法,是在单元子类中实现按钮操作,由委托返回 strong>按钮触摸的单元格。

我解释了各种方法here,最后一个是这个。

享受干净的代码良好的设计;)