ios - 自定义 UITableViewCells 和 UITableView 的问题

标签 ios objective-c uitableview

我已经在 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/

相关文章:

ios - AudioKit:如何更改使用 AKMIDIEvent 播放的音符的音量或音调?

ios - Swift 3 中的索引超出范围

c++ - 当我从 C++ 中的文件中读取时,显示数字的次数比它应该的多

ios - UImenucontroller Action 使用委托(delegate)来设置 tableView 正在编辑不工作_swift

ios - 如何将步进值传递给 ViewController?

objective-c - +(void)initialize方法中的alloc-init对象可以多次重用

objective-c - 在 View Controller 和 OS X 之间转换

objective-c - Plist 序列化导致内存泄漏

objective-c - IP地址? - cocoa

ios - 在 tableView 中添加两个自定义单元格