Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在TableView单元格创建中向UIButton分配操作的问题_Ios_Objective C_Uitableview_Uibutton - Fatal编程技术网

Ios 在TableView单元格创建中向UIButton分配操作的问题

Ios 在TableView单元格创建中向UIButton分配操作的问题,ios,objective-c,uitableview,uibutton,Ios,Objective C,Uitableview,Uibutton,我正在使用从服务器调用的数据动态填充表视图。我现在有两个主文件,一个用于新单元格,另一个用于在分配[cell.button addTarget:self action:@selector(addFriend:)forControlEvents:UIControlEventTouchUpInside]时处理TableView中的单元格填充到单元格中的我的按钮forrowatindexpath,我输入一个输出来记录点击按钮的动作,什么都没有发生 以下是我正在做的: - (UITableViewCel

我正在使用从服务器调用的数据动态填充
表视图。我现在有两个主文件,一个用于新单元格,另一个用于在分配
[cell.button addTarget:self action:@selector(addFriend:)forControlEvents:UIControlEventTouchUpInside]时处理
TableView
中的单元格填充
单元格中的我的按钮forrowatindexpath
,我输入一个输出来记录点击按钮的动作,什么都没有发生

以下是我正在做的:

- (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];
建议

更新:

这是我的
cellforrowatinexpath

- (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;
}

前提是您应该提供更多的代码来清楚地了解正在发生的事情, 这不是最好的办法

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

但无论如何,功能和设计的正确方法是在单元格子类中执行按钮操作,并由代表返回按钮被触摸的单元格

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


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

在哪里创建UIButton?您正在创建UITableViewCell的子类吗?你能粘贴那个代码吗?另外,我会尝试添加didSelectRowAtIndexPath方法,以查看当您点击该行时,该行是否实际被选中,而不是按钮。@JoshGafni我有
didselectro…
,它确实有效。cell.button是IBOutlet吗?@christruman不,我不相信。我给按钮目标定义的唯一地方是当我使用
[cell.button addtarget…]
好的,那么当您尝试添加目标时,按钮可能不存在。如果按钮为nil,则添加目标不会起任何作用。您可以在addTarget行的正前方放置一个断点,然后单击po cell.button,查看其nil.{{引文需要}}是否在
最佳方法中更改了所谓的“正确方法”
:)确定吗?@matteogobi我仍然不知道它为什么不能正确运行。检查更新在我的帖子中我说了另一件事:你必须在你的子类中配置按钮。因此,按钮的目标在子类中,从那里,通过委托模式,您将单元格(按钮接触的位置)返回给委托(即您的viewController)
#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