저는 iOS 개발의 초보자입니다. 현재 Matthijs Hollemans의 iOS Apprentice에서 제공하는 자습서를 통해 학습 할 수 있습니다. Xcode 6 및 Objective-C를 사용하여 자습서 응용 프로그램을 만들고 있습니다. 텍스트 "레이블이있는 사용자 정의 레이블 : 책의 두 번째 튜토리얼에서는 iOS 표보기 - 셀 또는 라벨에 태그를 설정 하시겠습니까?
, 그는 테이블 뷰, 네비게이션 컨트롤러 등 태초에을과해야 할 일 목록 응용 프로그램을 작성하는 방법을 독자를 가르치는 "표의 프로토 타입 셀에 배치됩니다. 이 시점에서 앱을 실행하면 예상대로 '라벨'이 앱에 표시됩니다.
테이블 뷰에 사용자 정의 텍스트를 표시하기 위해 그는 독자에게 레이블에 '태그'식별자를 설정하고 아래 코드에서와 같이 '태그'를 사용하여 사용자 정의 텍스트를 표시하도록 요청합니다. 이것은 비정상적인 행동으로 이어집니다.
이것은 앱을 실행할 때 '라벨'을 표시한다는 것입니다. 그러나 화면에서 텍스트를 스크롤 한 다음 다시 화면에 표시하면 사용자 정의 텍스트가 표시됩니다.
레이블에 '태그'식별자를 설정하는 대신이 작업을 특별히 경고하는 책의 저자 임에도 불구하고 표보기 셀에 설정합니다. 또한 사용자 정의 텍스트와 "레이블"이 겹치지 않도록 테이블 뷰의 레이블에서 기본 텍스트 "레이블"을 제거해야했습니다.
이 문제가 해결되었지만 이제 테이블보기를 사용할 때 '태그'식별자를 설정하기 위해 따라야 할 올바른 프로토콜이 무엇인지 혼란 스럽습니다. 표보기 셀 또는 레이블에 '태그'를 설정해야합니까? 그것이 라벨에 설정되어야한다면, 내가이 문제에 부딪히는 이유가 무엇일까요? 여기
주 ViewController.m#import "ViewController.h"
#import "ChecklistItem.h"
@interface ViewController()
@end
@implementation ViewController
{
NSMutableArray * _items;
}
- (void)viewDidLoad {
[super viewDidLoad];
_items = [[NSMutableArray alloc] initWithCapacity:20];
ChecklistItem * item;
item = [[ChecklistItem alloc]init];
item.text = @"Walk the dog";
item.checked = NO;
[_items addObject:item];
item = [[ChecklistItem alloc]init];
item.text = @"Brush teeth";
item.checked = NO;
[_items addObject:item];
item = [[ChecklistItem alloc]init];
item.text = @"Prepare breakfast";
item.checked = NO;
[_items addObject:item];
item = [[ChecklistItem alloc]init];
item.text = @"Soccer practice";
item.checked = NO;
[_items addObject:item];
item = [[ChecklistItem alloc]init];
item.text = @"Eat ice cream";
item.checked = NO;
[_items addObject:item];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
//data source method no.1 to get the number of rows in section for the table view
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_items count];
}
- (void)configureCheckmarkForCell:(UITableViewCell *)cell withChecklistItem:(ChecklistItem *) item {
//if the item is checked, display checkmark
if (item.checked) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
- (void)configureTextForCell:(UITableViewCell *)cell withChecklistItem:(ChecklistItem *) item {
UILabel * label = (UILabel *)[cell viewWithTag:1000];
label.text = item.text;
}
//data source mathod no.2 to get the cell to display the row in the given index path
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"];
ChecklistItem * item = _items[indexPath.row];
[self configureTextForCell:cell withChecklistItem:item];
[self configureCheckmarkForCell:cell withChecklistItem:item];
return cell;
}
//delegate method to handle taps on rows
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
ChecklistItem * item = _items[indexPath.row];
[item toggleChecked];
[self configureCheckmarkForCell:cell withChecklistItem:item];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
태그를 사용하지 않고, 당신은 셀을 구성하기위한 프로젝트의 간단한 버전을 다운로드 할 수 있습니다
을 추가 할
편집 당신이이 뜻, '셀에 방법이' 셀의 레이블을 콘센트로 사용 하시겠습니까? – user2278764
그건 그걸하는 방법 중 하나지만, 다시, 그냥 셀에 텍스트를주고 그것의 레이블에 그 텍스트를 넣는 방법을 알고 보자. – Abizern
대체 방법은 셀에 사용자 지정 항목을 전달하고 모든 항목을 표시하도록하는 것입니다. – Abizern