Iphone 帮助我获取IndexPath.row值

Iphone 帮助我获取IndexPath.row值,iphone,uitableview,ios4,Iphone,Uitableview,Ios4,我使用uiimageview创建了一个复选框,并将该复选框放入uitableview单元格,如下所示 选中该复选框时,我希望获得indexpath.row 所以我把UIImageViview添加到了牢房里。因此调用了didSelectRowatineXpath,并给出了indexpath.row 但是当选择行时,我想显示详细视图 现在这让我陷入了麻烦 所以你们能建议我如何解决上述问题吗 当我选中复选框时,我想得到indexpath.row 当选择行时,我需要显示详细视图 谢谢你的时间和帮助 更新

我使用uiimageview创建了一个复选框,并将该复选框放入uitableview单元格,如下所示

选中该复选框时,我希望获得indexpath.row

所以我把UIImageViview添加到了牢房里。因此调用了didSelectRowatineXpath,并给出了indexpath.row

但是当选择行时,我想显示详细视图

现在这让我陷入了麻烦

所以你们能建议我如何解决上述问题吗

当我选中复选框时,我想得到indexpath.row

当选择行时,我需要显示详细视图

谢谢你的时间和帮助

更新1:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

  static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  profileName = [appDelegate.archivedItemsList objectAtIndex:indexPath.row];

  if (cell == nil)
  {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:@"xc"] autorelease];

     cb = [[UIButton alloc] initWithFrame:CGRectMake(5,10, unselectedImage.size.width, unselectedImage.size.height)];

     [cb setImage:unselectedImage forState:UIControlStateNormal];
    [cb setImage:selectedImage forState:UIControlStateSelected];
    [cb addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
    [cell.contentView addSubview:cb];

}

if ( tableView == myTableView )
{
   titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 0, 150, 35)];
   titleLabel.font = [UIFont boldSystemFontOfSize:13];
   titleLabel.textColor = [UIColor blackColor]; 
    [cell.contentView addSubview:titleLabel];
   NSString *subjectData = [profileName.archive_subject stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
        [titleLabel setText:[NSString stringWithFormat: @"%@ ", subjectData]];

        lblDescription = [[UILabel alloc]initWithFrame:CGRectMake(60, 30, 210, 30)];
        lblDescription.numberOfLines = 2;
        lblDescription.lineBreakMode = YES;
        lblDescription.adjustsFontSizeToFitWidth = YES;
        lblDescription.font = [UIFont systemFontOfSize:10];
        lblDescription.textColor = [UIColor grayColor]; 
        [cell.contentView addSubview:lblDescription];
        NSString *CompanyName = [profileName.archive_content stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
        [lblDescription setText:[NSString stringWithFormat: @"%@ ", CompanyName]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   
    }


    return cell;
}

我认为是您的逻辑导致了
didselectrowatinexpath:
方法中的问题;确保这是正确的。此外,如果您只想对单元格使用复选标记,我认为最好使用
UITableViewCellAccessoryCheckmark
。可能会给你一个基本的想法

在复选框中使用UIButton而不是UIImageView-通过这种方式,您可以向其添加操作/方法,在其中可以获取indexPath,还可以为选中/未选中状态添加不同的图像,这将消除上述代码中出现的所有混乱内容:

因此,在您的
cellforrowatinexpath:
方法中:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...// Your existing code here

    UIImage *unselectedCheckboxImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"unselectedImageName" ofType:@"imageType"]];
    UIImage *selectedCheckboxImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"selectedImageName" ofType:@"imageType"]];

    UIButton *cb = [[UIButton alloc] initWithFrame:CGRectMake(desiredX, desiredY, unselectedCheckboxImage.frame.size.width, unselectedCheckboxImage.frame.size.height)];
    [cb setImage:unselectedCheckboxImage forState:UIControlStateNormal];
    [cb setImage:selectedCheckboxImage forState:UIControlStateSelected];
    [cb addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
    [cell.contentView addSubview:cb];
    [cb release];
}
然后,对于按钮操作方法:

- (IBAction)buttonAction:(id)sender
{
    if ([sender isKindOfClass:[UIButton class]])
    {
        UIButton *checkboxButton = (UIButton*)sender;
        checkboxButton.selected = !checkboxButton.selected;
        NSIndexPath *indexPath = [self.myTableView indexPathForCell:(UITableViewCell*)[[checkboxButton superview] superview]];
        // Do whatever you like here 
    }
}

也许你能解释到底出了什么问题?@Rog:谢谢你的代码,因为我是一个新手,你能解释一下你的代码是如何工作的吗?你要做的第一件事是在tableview单元格中添加一个按钮,然后将一个动作关联到它。在这种情况下,用户每次点击按钮,都会触发
按钮操作
方法。在这个方法中,我们通过调用superview或您的单元格superview(即实际的单元格)来获取indexpath。通过使用tableview方法
indexPathForCell
可以很容易地获得indexpath。有道理吗?如果不让我知道你到底不明白什么。干杯Rog@Rog:谢谢你的回复。当我滚动表格视图时,复选框的复选标记被覆盖了,我的意思是复选标记消失了。有什么想法吗?@Rog:你能帮我一下吗?你设置单元格的代码必须在if(cell==nil)语句中,所以当系统回收单元格时,它没有正确设置。发布更新后的cellForRowAtIndexpath代码:我来看看。