2012-11-09 4 views
0

죄송합니다. iOS를 처음 사용했습니다. 나는 UIAlertView 안에 UITableView을 만들려고합니다. 마지막으로 내가, 내가 UIAlertTableView 클래스 이런 식으로NSMutable Array를 DataSource로 사용하는 방법

UIAlertTableView *alert = [[UIAlertTableView alloc] initWithTitle:@"Choose a number" 
                  message:nil 
                 delegate:self 
               cancelButtonTitle:@"Cancel" 
               otherButtonTitles:nil, nil]; 
alert.tableDelegate = self; 
alert.dataSource = self; 
alert.tableHeight = 120; 
[alert show]; 

그러나 테스트 후를 구현 한

this tutorial를 입수했습니다 나는 UIAlert이 항목이 내부 나타나지와 빈 목록을 표시 얻었다. 이전에는 데이터 소스로 사용하려는 NSMUtableArray가 있습니다. 위의 튜토리얼에서 데이터 소스 할당은 alert.dataSource = self을 사용하여 완료 한 것으로 보입니다. 아직까지 NSMutableArray를 데이터 소스로 사용하는 방법과 관련하여 궁금해합니다. alert.dataSource과 관련이 있습니까?

+0

데이터 원본을 구현하고 tableview에 대한 메서드를 위임해야 할 수 있습니다. – iDev

답변

0

alertView의 테이블 데이터 소스 및 위임 파일로 새 파일을 만들어야한다고 제안합니다. 는 구현 :

@interface MyTableSource: UIViewController <UITableViewDataSource, UITableViewDelegate> 
@end 

@implementation MyTableSource 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

return 7; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableViews cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"MyIdentifier"; 


    UITableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.text = @"M"; 
    return cell; 

} 

@end 

같은 경고를 만듭니다

UIAlertTableView *alert = [[UIAlertTableView alloc] initWithTitle:@"Choose a number" 
                  message:nil 
                 delegate:self 
               cancelButtonTitle:@"Cancel" 
               otherButtonTitles:nil, nil]; 
MyTableSource *data = [[MyTableSource alloc] init]; 
alert.tableDelegate = data; 
alert.dataSource = data; 
alert.tableHeight = 120; 
[alert show]; 

참고 : 내가 alertView 테스트를 위해, 문제가 많이 있다는 것을 구현했습니다. alertView 클래스에서 [self setNeedsLayout]; 대신

[self layoutAnimated:YES];으로 전화해야합니다.

0

당신은 당신의 클래스는 데이터 소스 역할을한다 UIAlertView 말했지만 당신은 또한 최소한 다음 jQuery과 데이터 소스 메소드를 오버라이드 (override) 할 필요가 여기 mutableArray는 데이터 소스로 사용하려는 가변 배열을 의미한다 :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [mutableArray count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    // Set up the cell... 
    NSString *cellValue = [mutableArray objectAtIndex:indexPath.row]; 
    cell.text = cellValue; 
    return cell; 
} 
+0

나는 내 게시물에 약간의 수정을가 했으므로 알아 내기가 더 쉬울 것으로 기대한다. – mrkhv

+0

내 UIAlertTableView는 UITableViewController.m 클래스 내의 UITableView 항목을 클릭하여 표시되도록 만들어졌습니다. 나는 당신이 그 UITableViewController에 대해 말한 메소드를 구현했다. 하지만 그것은 내 UITableView, UIAlertTableView에 대한 데이터 소스를 제공하는 데 사용됩니다. 그래서 위의 코드를 UITableViewController.m 클래스 나 UIAlertTableView.m에 어디에 두어야하나요? – mrkhv

관련 문제