2

在我的UITableViewCell我有一个UIButton代表一个电话,当用户触摸它时,假设它会拨打由表格单元格中的特定对象指定的电话号码。 在此处输入图像描述

由于我将有多个按钮,并且每个按钮都有一个特定的电话号码,我试图将tag属性设置UIButton为 current 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];
    }
}
4

3 回答 3

7

标签确实不是处理这个问题的正确方法。您最好使用 MyPerson 的属性对 UITableViewCell 进行子类化。 [注意:如果合适,将 Person 替换为 School]像这样:

@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,添加 aUIButton并将按钮操作链接到单元格。

于 2013-05-14T21:52:36.960 回答
0

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

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

于 2013-05-14T22:46:44.547 回答
-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

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

于 2013-05-18T09:07:30.990 回答