gpt4 book ai didi

iOS (iPhone/iPad) SDK - UITableView 在 reloadData 上不刷新(numberOfSectionsInTableView 等不会在 reloadData 上被调用)

转载 作者:行者123 更新时间:2023-12-01 18:32:39 25 4
gpt4 key购买 nike

出于某种原因[tView reloadData] (其中 tView 是 UITableView)不会刷新我的 UITableView。 cellForRowAtIndexPath , numberOfSectionsInTableViewnumberOfRowsInSection只被调用一次 - 在加载时。在 [tView reloadData] 之后似乎没有调用这些方法.这是我的代码:

(AppDelegate.h):

#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"

@class FirstNavViewController;
@class SecondTableViewController;

@interface <appname>AppDelegate : NSObject <UIApplicationDelegate, MBProgressHUDDelegate> {
UIWindow *window;
UITabBarController *rootController;
FirstNavViewController *viewController;
SecondTableViewController *viewController1;
NSMutableData *responseData;
NSMutableArray *blogEntries;
MBProgressHUD *HUD;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *rootController;
@property (nonatomic, retain) IBOutlet FirstNavViewController *viewController;
@property (nonatomic, retain) IBOutlet SecondTableViewController *viewController1;
@property (nonatomic, retain) NSMutableArray *blogEntries;
@end

(AppDelegate.m):
#import "AppDelegate.h"
#import "FirstNavViewController.h"
#import "SecondTableViewController.h"
#import "SBJson.h"
#define TMP NSTemporaryDirectory()

@implementation AppDelegate

@synthesize window = _window;
@synthesize rootController;
@synthesize viewController;
@synthesize viewController1;
@synthesize blogEntries;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
CGFloat width = self.rootController.view.bounds.size.width;
CGFloat height = self.rootController.view.bounds.size.height;
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, width, height)];
UIImage *imageView = [UIImage imageNamed:@"theme_frame.png"];
UIColor *kMainColor = [[UIColor alloc] initWithPatternImage:imageView];

[v setBackgroundColor:kMainColor];
[kMainColor release];
[self.rootController.tabBar insertSubview:v atIndex:0];
imageView = nil;
[v release];

responseData = [[NSMutableData data] retain];
blogEntries = [NSMutableArray array];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"ENTER_JSON_URL_HERE"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

NSAssert(nil != self.rootController, @"tab bar controller not hooked up!");

BOOL iPad = NO;
#ifdef UI_USER_INTERFACE_IDIOM
iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#endif

if (iPad) {
self.viewController = [[[FirstNavViewController alloc] initWithNibName:@"FirstNavViewController_iPad" bundle:nil] autorelease];
self.viewController1 = [[[SecondTableViewController alloc] initWithNibName:@"SecondTableViewController_iPad" bundle:nil] autorelease];
}
else {
self.viewController = [[[FirstNavViewController alloc] initWithNibName:@"FirstNavViewController_iPhone" bundle:nil] autorelease];
self.viewController1 = [[[SecondTableViewController alloc] initWithNibName:@"SecondTableViewController_iPhone" bundle:nil] autorelease];
}

self.rootController.viewControllers = [NSArray arrayWithObject:self.viewController];

self.rootController.selectedIndex = 0;

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
self.window.rootViewController = self.rootController;
#else
[self.window addSubview:rootController.view];
#endif

[self.window makeKeyAndVisible];

HUD = [[MBProgressHUD alloc] initWithView:viewController.view];
[viewController.view addSubview:HUD];
[HUD show:NO];

// Regisete for HUD callbacks so we can remove it from the window at the right time
HUD.delegate = self;

HUD.labelText = @"Loading";

return YES;
}

#pragma mark NSURLConnection delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[HUD hide:YES];
[connection release];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];

NSMutableArray *allBlogEntries = [responseString JSONValue];
[viewController1 setBlogEntries:allBlogEntries];
[responseString release];
[HUD hide:YES];

}

- (void)dealloc
{
[_window release];
[rootController release];
[viewController release];
[viewController1 release];
[super dealloc];
}

@end

(FirstNavViewController.h):
#import <UIKit/UIKit.h>

@interface FirstNavViewController : UIViewController {
UINavigationController *navController;

}
@property (nonatomic, retain) UINavigationController *navController;
@end

(FirstNavViewController.m):
#import "FirstNavViewController.h"
#import "SecondTableViewController.h"

@implementation FirstNavViewController

@synthesize navController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
UITabBarItem *tabBarItem = [self tabBarItem];
UIImage *tabBarImage = [UIImage imageNamed:@"blog.png"];
[tabBarItem setImage:tabBarImage];
[tabBarItem setTitle:@"Blog"];
}
return self;
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
navController = [[UINavigationController alloc] initWithRootViewController:self];
SecondTableViewController *secondViewController = [[SecondTableViewController alloc] initWithNibName:@"BlogOverviewViewController_iPhone" bundle:nil];
[navController pushViewController:secondViewController animated:NO];
[blogOverviewViewController release];
[self.view addSubview:navController.view];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

(SecondTableViewController.h):
#import <UIKit/UIKit.h>
#import "SBJson.h"

@interface SecondTableViewController : UIViewController {
NSMutableArray *blogEntries;
IBOutlet UITableView *tView;
}
@property (nonatomic, retain) NSMutableArray *blogEntries;
@property (nonatomic, retain) NSArray *arryData;
@property (nonatomic, retain) IBOutlet UITableView *tView;
@end

(SecondTableViewController.m):
#import "SecondTableViewController.h"
#import "ThirdDetailViewController.h"
#import "NSString+HTML.h"

NSString *convertedString;

@implementation SecondTableViewController

@synthesize arryData;
@synthesize tView;


-(NSMutableArray*)blogEntries {
return [[blogEntries retain] autorelease];
}

-(void)setBlogEntries:(NSMutableArray*)newBlogEntries {
if(newBlogEntries != blogEntries) {
[newBlogEntries retain];

[blogEntries release];
blogEntries = newBlogEntries;

[tView reloadData];
}
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [blogEntries count];
}

// Customize the appearance of table view cells.
- (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...
NSDictionary *aBlogEntry = [blogEntries objectAtIndex:[indexPath row]];
NSArray *bPosts = (NSArray *)[aBlogEntry objectForKey:@"posts"];

NSString *stringToConvert = [bPosts valueForKey:@"title_plain"];
NSString *convertedString = [stringToConvert stringByConvertingHTMLToPlainText];

cell.textLabel.text = convertedString;
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.minimumFontSize = 10;
cell.textLabel.numberOfLines = 4;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

NSString *stringToConvert1 = [bPosts valueForKey:@"excerpt"];
NSString *convertedString1 = [stringToConvert1 stringByConvertingHTMLToPlainText];

cell.detailTextLabel.text = convertedString1;

return cell;
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ThirdDetailViewController *detailViewController = [[ThirdDetailViewController alloc] initWithNibName:@"BlogContentViewController_iPhone" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
NSDictionary *aBlogEntry = [blogEntries objectAtIndex:[indexPath row]];
NSArray *bPosts = (NSArray *)[aBlogEntry objectForKey:@"posts"];

NSString *stringToConvert = [bPosts valueForKey:@"title"];
NSString *convertedString = [stringToConvert stringByConvertingHTMLToPlainText];

[contentViewController changeTitleTextLabel:convertedString];

NSString *stringToConvert1 = [bPosts valueForKey:@"content"];
NSString *convertedString1 = [stringToConvert1 stringByConvertingHTMLToPlainText];

NSString *newConvertedString1 = [convertedString1 stringByReplacingOccurrencesOfString: @"\n" withString:@"\n\n"];

[detailViewController changeContentTextLabel:newConvertedString1];

[tableView deselectRowAtIndexPath: indexPath animated: YES];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
blogEntries = [[NSMutableArray alloc]init];
self.title = @"Blog";
[self.navigationItem setHidesBackButton:YES animated:NO];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
[blogEntries release];
[super dealloc];
}

@end

有谁知道问题是什么?我不知道为什么 UITableView 不会重新加载数据。

编辑:小背景:我正在使用带有 UIViewController 的 UITabBarController (AppDelegate),其中 UINavigationController 作为 UIViewController (FirstNavViewController) 的 subview 。 UINavigationController 然后有一个 subview ,其中包含另一个 UIViewController。后者的UIViewController(SecondTableViewController)包含了一个UITableView。

最佳答案

我刚遇到这个问题。结果对我来说是一个线程问题。对于那些在搜索后最终来到这里的人,这是我的快速修复:

[tView performSelectorOnMainThread:@selector(reloadData) 
withObject:nil
waitUntilDone:false];

似乎 reloadData 需要在主线程上调用。我希望这会有所帮助(^_^)

关于iOS (iPhone/iPad) SDK - UITableView 在 reloadData 上不刷新(numberOfSectionsInTableView 等不会在 reloadData 上被调用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7053623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com