自定义UITableViewCells和UITableView的问题

时间:2014-05-13 12:59:15

标签: ios objective-c uitableview

我已在Twitter中实施了UITableViewController Feed,并且我正在使用其中包含UITextView的自定义单元格,因此它具有链接检测功能。我遇到的问题是,只有第一个单元格出现,如果我开始滚动应用程序在EXC_BAD_ACCESS上使用cell.tweetText.text = t[@"text"];崩溃。如果我不使用自定义单元格,则Feed会正确显示,但是您无法点击链接。

TweetCell.h

#import <UIKit/UIKit.h>

@interface TweetCell : UITableViewCell <UITextViewDelegate>

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

@end

tableviewcontroller.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

1 个答案:

答案 0 :(得分:3)

替换行代码

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];
}