2011-08-31 86 views
0

我有两个关于内容视图的问题。我如何知道哪些内容视图被触动?

1问题: 有在的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? 
} 

回答

0

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)由于要重复使用电池,不要忘了零或内容视图unneedfull观点删除(因为他们已经在上一节中添加和第三个重复使用)。

UIView *view2remove = [cell viewWithTag:itsTag]; 
[view2remove removeFromSuperview]; 
+0

请告诉我的代码。我是初学者。 – user698200

+0

更新了我的答案。 1)在Apple文档中搜索UIGestureRecognizer,这很简单。 2)看起来更新文字 – Nekto

+0

谢谢。第二个问题解决了。我认为如果我知道哪个视图被触摸,就使用UIGestureRecognizer。 – user698200

0

您发布的代码缺少大括号。在else之前你需要额外的大括号。推测你的真实代码不是,或者它不会编译。

在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]; 
+0

谢谢。第二个问题解决了。但我仍然不知道第一个。 – user698200

相关问题