몇 가지 옵션이 있습니다.
옵션 1UIButtons의 2 차원 배열.
- (void)setupButtons
{
int N = 3;
int M = 3;
CGFloat buttonSize = 50.0f;
CGFloat padding = 5.0f;
CGPoint startButtonLocation = CGPointMake(40.0f, 40.0f);
CGPoint buttonLocation;
for (int n = 0; n < N; n++)
{
buttonLocation.x = startButtonLocation.x + n * (buttonSize + padding);
for (int m = 0; m < M; m++)
{
buttonLocation.y = startButtonLocation.y + m * (buttonSize + padding);
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(buttonLocation.x,
buttonLocation.y,
buttonSize,
buttonSize)];
button.backgroundColor = [UIColor grayColor];
[button setTitle:[NSString stringWithFormat:@"%i, %i", n, m]
forState:UIControlStateNormal];
[button addTarget:self
action:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
}
- (void)buttonTapped:(id)sender
{
NSLog(@"tapped index: %@", ((UIButton *)sender).titleLabel.text);
}
출력 : UICollectionView을 구현
옵션 2..
@interface ViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
{
UICollectionView *_collectionView;
}
@implementation
- (void)viewDidLoad
{
[super viewDidLoad];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[_collectionView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:_collectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 15;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor greenColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(50, 50);
}
출력 : 사용자가 항목을 탭하면
, 당신은 그것을 처리 할 수
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// Handling
}
나는 사용자 정의 테이블을 사용하고자하는 가장 중요한 것은 - 추적 터치 포인트를 강조 표시하고 해당 행과 열을 강조 표시합니다. UICollectionView를 사용하면이 작업을 수행 할 수 없습니다. –
가능합니다. 당신은'UICollectionView'로 이것을 달성 할 수 있습니다. 사용자가'UICollectionView'에서 항목을 선택하면'collectionView : didSelectItemAtIndexPath :' 메서드가 호출됩니다. – RaffAl
내 대답을 수락 해 주셔서 감사합니다. 당신도 upvote 수 있습니까? :) – RaffAl