在cellForRowAtIndexPath中未正确设置UIButton标记

时间:2013-05-14 21:38:43

标签: iphone ios objective-c

在我的UITableViewCell我有一个UIButton代表一个手机,当用户触摸时,它会调用表格单元格中特定对象指定的电话号码。 enter image description here

由于我将有多个按钮,并且每个按钮都有一个特定的电话号码,我尝试将tag上的UIButton属性设置为当前indexPath.row,但它适用于前6个单元格但是当单元格开始被重用时,标签只会返回到所有按钮的0。我正在重用块之外设置按钮的标签,我认为这应该是正确的方法来识别每个独特的单元格。

为了进行测试,我还将cell.tag属性设置为indexPath.row并且它完美地运行,因此它肯定与UIButton隔离。关于我可能做错的任何想法?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SchoolCellIdentifer = @"SchoolCellIdentifier";

    SchoolInfoItem *item = [self.schoolArray objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SchoolCellIdentifer];


    // If the cell doesn't existing go ahead and make it fresh.
    if (cell == nil)
    {        
        // Begin Phone button view
        UIButton *phoneButton = [[UIButton alloc] initWithFrame:CGRectMake(67, 70, 35, 35)];
        phoneButton.tag = 80;
        UIImageView *phoneView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"phone-icon.png"]];
        phoneView.frame = CGRectMake(2.5, 2.5, 30, 30);
        [phoneButton addSubview:phoneView];
        [cell.contentView addSubview:phoneButton];

    }

    cell.tag = indexPath.row;
    phoneButton = (UIButton *) [cell viewWithTag:80];
    [phoneButton addTarget:self action:@selector(callPhoneNumber:) forControlEvents:UIControlEventTouchUpInside];
    phoneButton.tag = cell.tag;
    NSLog(@"phoneButton tag: %d", phoneButton.tag);
    NSLog(@"cell tag:%d", cell.tag);

return cell
}

以下是TouchEvent的方法:

- (void) callPhoneNumber:(id)sender
{
    UIButton *button = (UIButton *) sender;
    SchoolInfoItem *schoolItem = [self.schoolArray objectAtIndex:button.tag];
    NSLog(@"tag at: %d", button.tag);

    if ([schoolItem.MainPhone length] != 0)
    {
        NSString *URLString = [@"tel://" stringByAppendingString:schoolItem.MainPhone];
        NSURL *URL = [NSURL URLWithString:URLString];
        NSLog(@"%@", URL);

        [[UIApplication sharedApplication] openURL:URL];
    }
}

3 个答案:

答案 0 :(得分:7)

标签确实不是处理此问题的正确方法。你最好使用MyPerson的属性为UITableViewCell创建子类。 [注意:如果合适,请将学生替换为学校] 喜欢这样:

@interface MyPerson : NSObject
@property (...) NSString *phoneNumber;
@property (...) NSString *emailAddress;
@end

@interface MyPersonCell : UITableViewCell
@property (readwrite) MyPerson *person;
- (IBAction) callPhoneNumber;
- (IBAction) sendEmail;
@end

然后在您的tableView:cellForRowAtIndexPath:实施中,将单元格配置为

cell.person = [get person from datasource for section+row];

此外,UIButton操作链接到单元格本身(如上面的代码所暗示)或表格视图控制器本身。

如果您使用Xcode故事板,则需要使用“原型单元格”,使用MyPersonCell进行配置,添加UIButton并将按钮操作链接到单元格。

答案 1 :(得分:0)

处理此问题(而不是使用标记)的另一种方法是,每当按下按钮时,处理它就像下一页所示:https://stackoverflow.com/a/1802875/250164

数据源确实应该由Person对象组成,作为GoZoner的建议。

答案 2 :(得分:-1)

if (cell == nil)
{        
    // Begin Phone button view
    UIButton *phoneButton = [[UIButton alloc] initWithFrame:CGRectMake(67, 70, 35, 35)];
    phoneButton.tag =indexpath.row; // try it ..
    UIImageView *phoneView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"phone-icon.png"]];
    phoneView.frame = CGRectMake(2.5, 2.5, 30, 30);
    [phoneButton addSubview:phoneView];
    [cell.contentView addSubview:phoneButton];

}

cell.tag = indexPath.row;

[phoneButton addTarget:self action:@selector(callPhoneNumber:)      forControlEvents:UIControlEventTouchUpInside];


NSLog(@"tag is :%d", phoneButton.tag);

 return cell

尝试使用我在代码中编辑的代码。我希望这段代码对你有用。