开发者

How do I know which content view is touched?

I have two questions about content view.

1st question: There are two content views in tableview cell. How do I know which one is touched?

2nd question: I only want content view to appear in the first section of the tableview. But, when I scroll up tableview, content view appears in the third section also. How can I fix this problem?

- (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) You can add different recognizers to each view which will call different methods.

// 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) As you are reusing cell, don't forget to nil or remove from content view unneedfull views (as they already been added in previous section and reused in third one).

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


The code you posted is missing a brace. You need an extra close brace before the else. Presumably your real code is not, or it would not compile.

In the else clause, assigning something to local variables won't do anything.

You also need to do something if indexPath.section != 0. If you do nothing, it is possible you will get the content of a cell that was built earlier. If you want the views not to appear, you would have to remove them. Something like:

for (UIView *subview in cell.contentView.subviews)
    [subview removeFromSuperview];

But I think it will be easier if you just use a different cell identifier for section 1 and other sections. Then you will not get back cells that have been used for section 1 in section 3, and have to reconfigure them. Like:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜