分组列表中的cell的子视图tag标记,如label和button

+ (NSInteger)buttonTagWithSection:(NSInteger)section row:(NSInteger)row
{

    NSString *tagString = [NSString stringWithFormat:@"%d%@",section,[TagInGroupTable stringForFourLengthwithNumber:row]];
    return [tagString integerValue];
}

+ (NSString*)stringForFourLengthwithNumber:(NSInteger)number
{
    if (number<10) {
        return [NSString stringWithFormat:@"000%d",number];
    }else if(number<100)
    {
        return [NSString stringWithFormat:@"00%d",number];
    }else if(number<1000)    
        return [NSString stringWithFormat:@"0%d",number];
    else
        return [NSString stringWithFormat:@"%d",number];
}

+ (NSIndexPath *)indexWithTag:(NSInteger )tag
{
    NSInteger length = [[NSString stringWithFormat:@"%d",tag] length];
    NSString *sectionString = [[NSString stringWithFormat:@"%d",tag] substringToIndex:length-4];
    NSString *rowString = [[NSString stringWithFormat:@"%d",tag] substringFromIndex:length-4];
    return [NSIndexPath indexPathForRow:[rowString integerValue] inSection:[sectionString integerValue]];
}
//该片段来自于http://outofmemory.cn

通过cell中的Button来得到之Cell的IndexPath

[cell.btn addTarget:self action:@selector(cellBtnClicked:event:) forControlEvents:UIControlEventTouchUpInside];

- (void)cellBtnClicked:(id)sender event:(id)event {
    NSSet *touches =[event allTouches];
    UITouch *touch =[touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:_tableView];
    NSIndexPath *indexPath= [_tableView indexPathForRowAtPoint:currentTouchPosition];
    if (indexPath!= nil) {
        // do something
    }
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图