2014-02-22 8 views
1

N 개의 행과 M 열이있는 테이블이 필요합니다.행/열 강조 표시가있는 2 차원 테이블

테이블의 터치 이벤트로 인해 현재 열과 행이 강조 표시되고 해제되면 조치가 트리거됩니다.

이러한 테이블을 작성할 수있는 방법은 무엇입니까?

나는 터치 동작을 처리하기 위해 테이블을 만들기 위해 UIView/ UIButtons2D 배열을 사용하고,이 테이블을 통해 눈에 보이지 않는 UIView을 가하고 생각했다.

당신은 어떻게 생각하십니까? 아니면 더 나은 해결책이 있습니까?

답변

1

몇 가지 옵션이 있습니다.

옵션 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을 구현

enter image description here

옵션 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); 
} 

출력 : 사용자가 항목을 탭하면

enter image description here

, 당신은 그것을 처리 할 수 ​​

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Handling 
} 
+1

나는 사용자 정의 테이블을 사용하고자하는 가장 중요한 것은 - 추적 터치 포인트를 강조 표시하고 해당 행과 열을 강조 표시합니다. UICollectionView를 사용하면이 작업을 수행 할 수 없습니다. –

+0

가능합니다. 당신은'UICollectionView'로 이것을 달성 할 수 있습니다. 사용자가'UICollectionView'에서 항목을 선택하면'collectionView : didSelectItemAtIndexPath :' 메서드가 호출됩니다. – RaffAl

+0

내 대답을 수락 해 주셔서 감사합니다. 당신도 upvote 수 있습니까? :) – RaffAl

0

NSMutableArrayNSMutableArrays 인 매트릭스를 만들 수 있습니다.

그러나 당신이 성취하려고 시도한 것에 대해 설명하면, UICollectionView이 당신을 위해 해결할 수있는 것과 아주 흡사합니다. 코딩 포인트에서 이것은 거의 UITableView과 유사하지만 셀 그리드를 생성 할 수 있습니다.

콜렉션 뷰는 콜렉션 뷰가 단일 뷰 이외의 다른 뷰를 지원할 수 있다는 점을 제외하면 테이블 뷰와 동일한 일반 기능을 제공합니다. 열 레이아웃. 컬렉션 뷰는 다중 열 그리드, 타일 배치, 원형 레이아웃 등을 구현하는 데 사용할 수있는 사용자 정의 레이아웃을 지원합니다.

전체 documentation을 살펴보십시오.

관련 문제