gpt4 book ai didi

ios - 自定义 UITableViewCells 和 UITableView 的问题

转载 作者:行者123 更新时间:2023-11-28 20:01:57 25 4
gpt4 key购买 nike

我已经在 UITableViewController 中实现了一个 Twitter 提要,我正在使用一个带有 UITextView 的自定义单元格,所以它有链接检测。我遇到的问题是只有第一个单元格出现,如果我开始滚动应用程序崩溃,EXC_BAD_ACCESS on cell.tweetText.text = t[@"text"];。如果我不使用自定义单元格,提要会正确显示,但您无法点击链接。

TweetCell.h

#import <UIKit/UIKit.h>

@interface TweetCell : UITableViewCell <UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UITextView *tweetText;

@end

表格 View Controller .h

#import <UIKit/UIKit.h>

@interface GRSTwitterTableViewController : UITableViewController

@property (nonatomic, strong) NSMutableArray *twitterFeed;

@end

tableviewcontroller.m

#import "GRSTwitterTableViewController.h"
#import "TweetCell.h"
#import "STTwitter.h"

@interface GRSTwitterTableViewController ()

@end

@implementation GRSTwitterTableViewController
@synthesize twitterFeed;

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

self.title = @"";

self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];

UIBarButtonItem *openTwitterButton = [[UIBarButtonItem alloc]initWithTitle:@"Open in Twitter" style:UIBarButtonItemStylePlain target:self action:@selector(openTwitter:)];
self.navigationItem.rightBarButtonItem = openTwitterButton;

STTwitterAPI *twitter = [STTwitterAPI twitterAPIAppOnlyWithConsumerKey:@"xz9ew8UZ6rz8TW3QBSDYg" consumerSecret:@"rm8grg0aIPCUnTpgC5H1NMt4uWYUVXKPqH8brIqD4o"];

[twitter verifyCredentialsWithSuccessBlock:^(NSString *username)
{
[twitter getUserTimelineWithScreenName:@"onmyhonorband" count:50 successBlock:^(NSArray *statuses)
{
twitterFeed = [NSMutableArray arrayWithArray:statuses];
[self.tableView reloadData];
}
errorBlock:^(NSError *error)
{
NSLog(@"%@", error.debugDescription);
}];
}
errorBlock:^(NSError *error)
{
NSLog(@"%@", error.debugDescription);
}];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)openTwitter:(id)sender
{
NSURL *twitterURL = [NSURL URLWithString:@"twitter:///user?screen_name=onmyhonorband"];
NSURL *safariURL = [NSURL URLWithString:@"https://twitter.com/onmyhonorband"];

if ([[UIApplication sharedApplication]canOpenURL:twitterURL])
{
[[UIApplication sharedApplication]openURL:twitterURL];
}
else
{
[[UIApplication sharedApplication]openURL:safariURL];
}
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
tableView.backgroundColor = [UIColor darkGrayColor];

TweetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if (!cell)
{
[tableView registerNib:[UINib nibWithNibName:@"TweetCell" bundle:nil] forCellReuseIdentifier:@"myCell"];
cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
}
NSInteger idx = indexPath.row;
NSDictionary *t = twitterFeed[idx];

cell.tweetText.text = t[@"text"];

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

这是使用自定义单元格时 tableview 的屏幕截图。 enter image description here

最佳答案

替换行代码

TweetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if (!cell)
{
[tableView registerNib:[UINib nibWithNibName:@"TweetCell" bundle:nil] forCellReuseIdentifier:@"myCell"];
cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
}

使用以下代码

TweetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if (!cell)
{
NSArray *nibs =[[NSBundle mainBundle] loadNibNamed:@"TweetCell" owner:self options:NULL];
cell = [nibs firstObject];
}

关于ios - 自定义 UITableViewCells 和 UITableView 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23631984/

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