【问题标题】:UITableView - methods are not being calledUITableView - 没有调用方法
【发布时间】:2012-07-30 21:56:04
【问题描述】:

我有一个子视图,我在其中构建了一个 UITableview,我已将委托和数据源设置为子视图,但由于某种原因没有调用表方法...有人可以查看我的代码,看看我是什么做错了吗?谢谢

.h 文件

@interface TwitterController : UIView <UITableViewDelegate, UITableViewDataSource> { 

UIButton* btnCloseView; 
UITableView* tblTweets;
UIImageView* imgTwitterIcon;

ColorController* colorManager;

NSMutableArray* tweetsArray;
NSMutableArray* tableData;

NSString* twitterID;

}

@property (nonatomic, retain) NSString* twitterID;

- (NSMutableArray* ) getTweets; 

- (void) sendNotification : (id) sender;

@end

.m 文件

#import "TwitterController.h"

@implementation TwitterController

@synthesize twitterID;

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {

    colorManager = [ColorController new];

}
return self;
}


/*- (void)drawRect:(CGRect)rect {



}*/

- (void)layoutSubviews { 

imgTwitterIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imgTwitterBird"]];
CGRect twitterIconFrame = [imgTwitterIcon frame];
twitterIconFrame.origin.x = 50.0;
twitterIconFrame.origin.y = 20.0;

tblTweets = [[UITableView alloc] initWithFrame:CGRectMake(50.0, 25.0, 220.0, 500.0)];
tblTweets.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tblTweets.separatorColor = [colorManager setColor:176.0:196.0:222.0];
tblTweets.layer.borderWidth = 1.0;
tblTweets.rowHeight = 20.0;
tblTweets.scrollEnabled = YES;
tblTweets.delegate = self;
tblTweets.dataSource = self;

tableData = [self getTweets];

UIImage* imgCloseButton = [UIImage imageNamed:@"btnCloseWindow.png"];
CGSize imageSize = imgCloseButton.size;
btnCloseView = [[UIButton alloc] initWithFrame: CGRectMake(220.0, 550.0, imageSize.width, imageSize.height)];
[btnCloseView setImage:[UIImage imageNamed:@"btnCloseWindow.png"] forState:UIControlStateNormal];
[btnCloseView addTarget:self action:@selector(sendNotification:) forControlEvents:UIControlEventTouchUpInside];


[self addSubview:tblTweets];
[self addSubview:imgTwitterIcon];
[self addSubview:btnCloseView];
}

- (NSMutableArray*) getTweets { 

//array to hold tweets
tweetsArray = [[NSMutableArray alloc] init];

twitterID = @"Pruit_Igoe";

///set up a NSURL to the twitter API
NSURL* twitterAPI = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=%@&count=10", twitterID]];

//get last 10 tweets (max is 20)
TWRequest *twitterRequest = [[TWRequest alloc] initWithURL:twitterAPI 
            parameters:nil requestMethod:TWRequestMethodGET];

// Notice this is a block, it is the handler to process the response
[twitterRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
    if ([urlResponse statusCode] == 200)  {

        // The response from Twitter is in JSON format
        // Move the response into a dictionary and print
        NSError *error;        
        NSDictionary *tweetsDict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];

        for(NSDictionary* thisTweetDict in tweetsDict) { 

            [tweetsArray addObject:[thisTweetDict objectForKey:@"text"]];
        }

    }
    else
        NSLog(@"Twitter error, HTTP response: %i", [urlResponse statusCode]);
}];

return tweetsArray;


}

#pragma mark Table Management

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [tableData count];
NSLog(@"%i", [tableData count]); //does not log!
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section    {
return [tableData count];
}

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

static NSString *CellIdentifier = @"tableCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.textLabel.textColor = [UIColor colorWithRed:66.0/255.0 green:66.0/255.0 blue:66.0/255.0 alpha:1];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size: 13.0];
cell.textLabel.text = [tweetsArray objectAtIndex:indexPath.row];
CGRect cellFrame = [cell frame];
cellFrame.size.height = 25.0;

return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSString* thisTweet = [tableData objectAtIndex:indexPath.row];
}

#pragma mark Close Window

- (void) sendNotification : (id) sender { 

NSMutableDictionary* userData = [[NSMutableDictionary alloc] init];

[userData setObject:@"closeTwitter" forKey:@"theEvent"];

[[NSNotificationCenter defaultCenter] postNotificationName:@"theMessenger" object:self userInfo: userData];

}

@end

【问题讨论】:

  • 你分配和初始化你的 tblTweets 了吗?
  • 他有 - 但在layoutSubviews

标签: iphone ios uitableview


【解决方案1】:

我认为你没有分配和初始化tableData。在- (id)initWithFrame:(CGRect)frame 方法或- (void)layoutSubviews 方法中写入tableData = [[NSMutableArray alloc] init];。试试看吧。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"%i", [tableData count]);
return [tableData count];
//NSLog(@"%i", [tableData count]); //does not log!
}

如果输出为零,则tableData 数组为空。检查它tableData数组

【讨论】:

  • 你太老套了,我错过了那个。
  • 如果 tweetArray 已分配/初始化并从 getTweets 返回,我是否必须分配/初始化 tableData?我以为我不必。我将 alloc/init 添加到我的代码中,但没有任何改变。
  • 输出不是0,根本没有输出。在 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [tableData count]; NSLog(@"%i", [tableData count]); //不记录! } 它根本不记录。
  • 写'NSLog(@"%i", [tableData count]);'在'返回 [tableData 计数];'之前。它会起作用的。
  • 呃。 :D 是的,得到 0,但数组中有 10 个项目。
【解决方案2】:

您应该在initWithFrame: 中进行所有初始化(例如tblTweets)。

layoutSubviews 用于布置子视图。

事实上,如果您将所有代码 fromLayoutSubviews 移动到 initWithFrame:,您的代码将(应该)工作。然后,您可以将部分代码(布局部分)移回。

编辑:当您移动初始化代码时,您可能还必须在 tableData = [self getTweets]; 之后添加 [tblTweets reloadData];

【讨论】:

  • 这是出于性能原因还是代码组织的最佳实践?我会在 init 或 layoutSubviews 中添加 UITableView 吗?即 - (void) layoutSubviews { [self addSubview tblTweets]; } ?
  • 关于为什么没有加载表数据的任何线索? :)
  • layoutSubviews 在应用程序执行期间可能会被多次调用 - 这是主要原因。我会检查加载。
  • 我正在查看文档,Apple 建议使用 viewDidLoad 添加子视图:developer.apple.com/library/ios/#documentation/windowsviews/…
  • 这种layoutSubviews 实现通常用于UIVIew 的更复杂的子类中。你绝对不应该在那个地方初始化和添加 tblTweets。每次调用它时,您的 tableView 都会重新初始化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-26
  • 2016-03-28
  • 2023-04-01
相关资源
最近更新 更多