【问题标题】:How to load data in tableView如何在tableView中加载数据
【发布时间】:2011-02-18 08:18:52
【问题描述】:

您好,我有以下代码,我试图在 UIAlertView 上添加 UITableView,为此我在 UIAlertTableView 中对 UIAlertView 进行了子类化。

#import "UIAlertTableView.h"

#define kTablePadding 8.0f


@interface UIAlertView (private)
- (void)layoutAnimated:(BOOL)fp8;
@end

@implementation UIAlertTableView

@synthesize dataSource;
@synthesize tableDelegate;
@synthesize tableHeight;
@synthesize tableView;

- (void)layoutAnimated:(BOOL)fp8 {
    [super layoutAnimated:fp8];
    [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y - tableExtHeight/2, self.frame.size.width, self.frame.size.height + tableExtHeight)];

    // We get the lowest non-control view (i.e. Labels) so we can place the table view just below
    UIView *lowestView;
    int i = 0;
    while (![[self.subviews objectAtIndex:i] isKindOfClass:[UIControl class]]) {
        lowestView = [self.subviews objectAtIndex:i];
        i++;
    }

    CGFloat tableWidth = 262.0f;

    tableView.frame = CGRectMake(11.0f, lowestView.frame.origin.y + lowestView.frame.size.height + 2 * kTablePadding, tableWidth, tableHeight);

    for (UIView *sv in self.subviews) {
        // Move all Controls down
        if ([sv isKindOfClass:[UIControl class]]) {
            sv.frame = CGRectMake(sv.frame.origin.x, sv.frame.origin.y + tableExtHeight, sv.frame.size.width, sv.frame.size.height);
        }
    }

}

- (void)show{
    [self prepare];
    [self.tableView reloadData];
    [super show];
}

- (void)prepare {
    if (tableHeight == 0) {
        tableHeight = 150.0f;
    }

    tableExtHeight = tableHeight + 2 * kTablePadding;

    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f) style:UITableViewStyleGrouped];
    tableView.delegate = tableDelegate;
    tableView.dataSource = dataSource;      

    [self insertSubview:tableView atIndex:0];

    [self setNeedsLayout];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)dealloc {
    [tableView release];
    [super dealloc];
}
@end

Now i want to load this tableView from data by calling reloadData in 'show' method but it is not calling delegate methods written in the class.

Also, i am showing the code of other class from where i am showing the alert.

- (void) showEsign{
    //IRPSaveRepairResponseDO *saveRepairDO = [[[[IRPSessionCache sharedInstance] currentSessionObject] dataModel] saveRepairDetails];
    IRPDataModel *dataModel = [[[IRPSessionCache sharedInstance] currentSessionObject] dataModel];
    IRPProductDetailsDO *prodDO = [dataModel productDetailsDO];
    //IRMLegalDocumentDO *legalDocDO = [dataModel legalDocDO];

    NSMutableArray *printLanguageArray = [prodDO languageOptionsDisplayArray];

    if ([printLanguageArray count] == 1) {
        DLog(@"prodDO.defaultLanguageDescription: %@", prodDO.defaultLanguage);
        [prodDO setUserSelectedLanguageForEsign:prodDO.defaultLanguage];
        UIAlertTableView *alert = [[UIAlertTableView alloc] initWithTitle:@"Select language"
                                                                  message:@"Concierge"
                                                                 delegate:self
                                                        cancelButtonTitle:kCancel
                                                        otherButtonTitles:kNext, nil];
        alert.tableDelegate = self;
        alert.dataSource = self;
        alert.tableHeight = 120;    
        [alert.tableView reloadData];
        [alert show];
    }
}

我想用一个数组加载这个 tableView。

【问题讨论】:

  • 那么您在实现这一目标时面临的问题是什么。
  • 恕我直言,将表格放入警报视图不是很好的用户体验。

标签: iphone objective-c uitableview


【解决方案1】:

这让我的大脑受伤了。

我不确定您在哪里初始化表格视图。也许它在您的 UIAlertView 子类之外。我的猜测是委托/数据源没有正确设置。您可能需要添加如下内容:

 -(void) setTableView:(UITableView *)newTableView {  
   [tableView release];
   tableView = [newTableView retain];   
   tableview.delegate = self; 
   tableView.dataSource = self;
  }

但是,话说回来,我喝醉了,如果我不理解,我很抱歉。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多