테이블보기가 있습니다. 각 셀은 사용자 정의 tablecell보기이며, 하위보기가 있으며, 하위보기는 CoreText 그리기 텍스트를 사용합니다. 나는 테이블 뷰의 (셀라라는 이름의) 하나 개의 셀을 누르면 서브 뷰의 클래스 이름 아래 CCoreTextViewUILongPressGestureRecognizer 처리기가 잘못된 개체를 사용합니다.
@interface CoreTextView()
{
CTFrameRef _frame;
}
@end
- (void) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UILongPressGestureRecognizer *myLongPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(myLongPressHandle:)];
[self addGestureRecognizer:myLongPressRecognizer];
[myLongPressRecognizer release];
}
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawText:context];
}
- (void)drawText:(CGContextRef)context
{
CTFontRef font = CTFontCreateWithName((CFStringRef)self.font.fontName, self.font.pointSize, NULL);
NSDictionary *attribs = [NSDictionary dictionaryWithObjectsAndKeys:(id)font, kCTFontAttributeName, nil];
NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc] initWithString:self.text attributes:attribs];
CFRelease(font);
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGFloat headIdent = CORETEXTVIEW_HEAD_IDENT;
CGFloat tailIdent = CORETEXTVIEW_TAIL_IDENT;
CTParagraphStyleSetting settings[] = {
{kCTParagraphStyleSpecifierAlignment, sizeof(_textAlignment), &_textAlignment },
{kCTParagraphStyleSpecifierLineSpacing, sizeof(CGFloat), &_leading},
{kCTParagraphStyleSpecifierFirstLineHeadIndent, sizeof(CGFloat), &_firstLineHeadIndent},
{kCTParagraphStyleSpecifierHeadIndent, sizeof(CGFloat), &headIdent},
{kCTParagraphStyleSpecifierTailIndent, sizeof(CGFloat), &tailIdent}
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings)/sizeof(settings[0]));
CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attribString,
CFRangeMake(0, [self.text length]),
kCTParagraphStyleAttributeName,
paragraphStyle);
CFRelease(paragraphStyle);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attribString);
CGRect columnFrame = CGRectMake(0,
0,
self.bounds.size.width,
self.bounds.size.height);
columnFrame = UIEdgeInsetsInsetRect(columnFrame, UIEdgeInsetsMake(0, CORETEXTVIEW_EDGE_INSET_LEFT, 0, CORETEXTVIEW_EDGE_INSET_RIGHT));
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, &CGAffineTransformIdentity, columnFrame);
CFRange textRange = CFRangeMake(0, 0);
if (NULL != _frame) {
CFRelease(_frame);
_frame = NULL;
}
_frame = CTFramesetterCreateFrame(framesetter, textRange, framePath, NULL);
CTFrameDraw(_frame, context);
CFRelease(framePath);
[attribString release];
CFRelease(framesetter);
}의 일부 코드이다 CCoreTextView입니다
- (void)myLongPressHandle:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint point = [gestureRecognizer locationInView:self];
NSArray *lineArr = (NSArray*)CTFrameGetLines(_frame);
NSInteger lineCount = [lineArr count];
NSLog(@"lineCount=%d, frame=%@, lineArr=%@", lineCount, _frame, lineArr);
}
문제는,하지 (CellB라는 이름의) 다른 셀로 손가락을 옮기면 longPressHandle은 여전히 CellA의 하위 뷰를 사용합니다. NSLog
은 CellA의 하위보기의 내용을 인쇄합니다.
테이블 뷰 코드를 게시 할 수 있습니까? –