0

申し訳ありませんが、iOSは初めてです。UITableView中身を作るつもりUIAlertViewです。最後に私はこのチュートリアルを手に入れました

UIAlertTableView私はこのようにクラスを実装しました

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];

ただし、テスト後、UIAlertに空白のリストが表示され、内部にアイテムが表示されませんでした。以前、データソースとして使用したいNSMUtableArrayがあります。上記のチュートリアルから、データソースの割り当てはを使用して行われているようalert.dataSource = selfです。それでも、NSMutableArrayをデータソースとして使用する方法と、それがどのように関連するのalert.dataSourceか疑問に思っています。

4

2 に答える 2

0

UIAlertViewに、クラスがデータソースとして機能することを伝えましたが、少なくとも次のUITableViewデータソースメソッドをオーバーライドする必要もあります。ここで、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;
}
于 2012-11-09T05:05:20.380 に答える
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を実装しましたが、多くの問題があります。電話する必要があります:

[self layoutAnimated:YES];[self setNeedsLayout];alertViewクラスの代わりに。

于 2012-11-09T06:25:37.147 に答える