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 回答