为什么随机生成的单词不保留?

时间:2014-05-25 14:07:10

标签: ios objective-c

我使用以下方法生成随机单词。 “wordsToMakeRandomEmail”是一个单词数组。我正在使用带有UITableViewController的storyboard。

还有一个搜索栏控制器。第一次生成随机电子邮件,并在UITableViewController的表视图中显示。但是当我选择一个单元格时,它显示为零。任何帮助都会很棒。

感谢。

-(NSString*)getRandomWord
{
  NSUInteger randomIndex = arc4random() % [wordsToMakeRandomEmail count];
  NSString *string = (wordsToMakeRandomEmail)[randomIndex];
  return [string copy];
}

-(NSString*)getRandomEmail
{
  NSString *emailPrefix = [self GetRandomWord];
  NSString *email = [NSString stringWithFormat:@"%@@google.com",emailPrefix];
  return [email copy];
}

以下方法用于表格单元格构建:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Configure the cell...
    NSMutableArray  *emails;
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        emails = (searchResults)[indexPath.row];
    }
    else
    {
        emails = (self.emailConversations)[indexPath.row];
    }

    EmailData *email = (emails)[0];

    if(email.IsReaden && tableView != self.searchDisplayController.searchResultsTableView)
    {
        ReadEmailTableViewCell *readCell;
        readCell = (ReadEmailTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:@"Read" forIndexPath:indexPath];
        readCell.Sender.text = email.Sender;
        readCell.Subject.text = email.Subject;
        readCell.Time.text = email.Time;
        NSString *inStr = [@([emails count]) stringValue];
        readCell.Count.text = inStr;
        return readCell;
    }
    else
    {
        UnReadEmailTableViewCell *unReadCell;
        unReadCell = (UnReadEmailTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:@"UnRead" forIndexPath:indexPath];

        NSMutableAttributedString *s =
        [[NSMutableAttributedString alloc] initWithString:email.Sender];

        [s addAttribute:NSBackgroundColorAttributeName
                  value:[UIColor yellowColor]
                  range:NSMakeRange(0, s.length)];

        unReadCell.Sender.attributedText = s;
        unReadCell.Subject.text = email.Subject;
        unReadCell.Time.text = email.Time;

        NSString *inStr = [@([emails count]) stringValue];
        unReadCell.Count.text = inStr;

        unReadCell.Line1.text = email.Line1;
        unReadCell.Line2.text = email.Line2;
        return unReadCell;
    }

    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        return 115;
    }
    NSMutableArray *emails = (self.emailConversations)[indexPath.row];
    EmailData  *email = (emails)[0];
    if(email.IsReaden)
    {
        return 61;
    }
    return 115;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

    if([segue.identifier isEqualToString:@"SettingsSegue"])
    {
        return;
    }

    NSIndexPath *indexPath = nil;
    EmailConversationTableViewController *destViewController = segue.destinationViewController;

    NSMutableArray *singleConversation;

    if (self.searchDisplayController.active)
    {
        indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
        singleConversation = (searchResults)[indexPath.row];
    }
    else
    {
        indexPath = [self.tableView indexPathForSelectedRow];
        singleConversation = (self.emailConversations)[indexPath.row];
    }

    destViewController.emails = singleConversation;
}

1 个答案:

答案 0 :(得分:0)

好的谢谢大家,在尝试蛮力之后,我已经能够解决问题了。

EmailData是由我创建的一个类。它有一些NSString属性。他们被宣布为weak。 Imho,这是内存泄漏的原因。我刚刚将其更改为strong,问题就解决了。

旧代码:

@property (nonatomic, weak) NSString *Sender;

更新代码:

@property (nonatomic, strong) NSString *Sender;