Iphone 我如何知道触动了哪个内容视图?

Iphone 我如何知道触动了哪个内容视图?,iphone,Iphone,关于内容视图,我有两个问题 第一个问题: tableview单元格中有两个内容视图。我怎么知道哪个被碰了 第二个问题: 我只希望内容视图出现在tableview的第一部分。 但是,当我向上滚动tableview时,内容视图也出现在第三部分中。 我如何解决这个问题 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { st

关于内容视图,我有两个问题

第一个问题: tableview单元格中有两个内容视图。我怎么知道哪个被碰了

第二个问题: 我只希望内容视图出现在tableview的第一部分。 但是,当我向上滚动tableview时,内容视图也出现在第三部分中。 我如何解决这个问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{       
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];   

    UIImageView *imgView, *imgView1;   
    if(cell == nil)   
    {
       if (indexPath.section == 0) {
           cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];     
           cell.textLabel.text = @"test";

           imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100,0,20,62)];
           [imgView setImage:[UIImage imageNamed:@"1.png"]];
           imgView.tag = 10;
           [cell.contentView addSubview:imgView];
           [imgView release];

           imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(200,0,20,62)];
           [imgView1 setImage:[UIImage imageNamed:@"2.png"]];
           imgView1.tag = 20;
           [cell.contentView addSubview:imgView1];
           [imgView1 release];
       }
    }   
    else
    {
        if (indexPath.section == 0) {
           imgView = (id)[cell.contentView viewWithTag:10];
           imgView1 = (id)[cell.contentView viewWithTag:20];
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    // How do I know left content view is touched or right content view is touched?
}
1) 您可以向每个视图添加不同的识别器,这些识别器将调用不同的方法

// create view
UIImageView *view = [UIImageView ...];
view.userInteractionEnabled = YES;
// create recognizer
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myViewTapped:)];
// add recognizer to your view
[view addGestureRecognizer:recognizer];
[recognizer release];
// now when user will tap on your view method - (IBAction)myViewTapped:(UIGestureRecognizer); will be called
2) 在重用单元时,不要忘记从内容视图中删除不必要的视图(因为它们已经在上一节中添加,并在第三节中重用)


您发布的代码缺少大括号。在进行其他操作之前,您需要一个额外的紧密支撑。可能您的真实代码不是,或者它不会编译

在else子句中,为局部变量赋值不会起任何作用

如果
indexPath.section!=0
。如果不执行任何操作,则可能会获得先前构建的单元格的内容。如果不希望视图出现,则必须将其删除。比如:

for (UIView *subview in cell.contentView.subviews)
    [subview removeFromSuperview];
但我认为,如果您只是在第1节和其他节中使用不同的单元格标识符,会更容易。然后,您将无法取回用于第3节第1节的单元格,必须重新配置它们。比如:

NSString *CellIdentifier;
if (indexPath.section == 0)
    CellIdentifier = @"Section1Cell";
else
    CellIdentifier = @"OtherSectionCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

更新了我的答案。1) 在apple文档中搜索UIgestureRecognitor,它非常简单。2) 听着,谢谢。第二个问题解决了。我想如果我知道触动了哪个视图,就会使用UIgestureRecognitor。更新了我的答案。我添加了创建识别器的示例代码谢谢。第二个问题解决了。但我还是不知道第一个。
NSString *CellIdentifier;
if (indexPath.section == 0)
    CellIdentifier = @"Section1Cell";
else
    CellIdentifier = @"OtherSectionCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];