【问题标题】:How can I use NSMutable Array as DataSource如何使用 NSMutable 数组作为数据源
【发布时间】:2012-10-29 10:10:49
【问题描述】:

抱歉,我是 iOS 新手。我打算在UIAlertView 中创建一个UITableView。最后我得到了this tutorial

我已经以这种方式实现了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 的关系?

【问题讨论】:

  • 您可能需要为 tableview 实现数据源和委托方法。

标签: ios uitableview nsmutablearray datasource uialertview


【解决方案1】:

我建议您需要创建一个新文件作为 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]; 而不是 alertView 类中的[self setNeedsLayout];

【讨论】:

    【解决方案2】:

    您已经告诉 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;
    }
    

    【讨论】:

    • 我对我的帖子做了一些修改,所以我希望能更清楚地弄清楚。
    • 我的 UIAlertTableView 是通过单击 UITableViewController.m 类中的 UITableView 项来创建的。我已经实现了你在那个 UITableViewController 上说的那些方法。但它用于为我的 UITableView 提供数据源,而不是 UIAlertTableView。那么,我应该把上面的代码放在我的 UITableViewController.m 类中还是 UIAlertTableView.m 中?
    猜你喜欢
    • 1970-01-01
    • 2011-02-16
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 2020-03-21
    相关资源
    最近更新 更多