2011-08-31 51 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

相關問題