【问题标题】:How make a UITableViewCell text like Facebook messenger for iOS?如何为 iOS 制作像 Facebook Messenger 一样的 UITableViewCell 文本?
【发布时间】:2014-10-31 08:37:24
【问题描述】:

当我们在 Facebook Messenger 上收到消息时,UITableViewCell 中的文本在触摸单元格并查看消息后变为粗体,然后返回 UITableView,单元格文本不再是粗体。

如何创建这种行为?

在我的代码下面:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
.
.
.
//Implementation
.
.
.

 [[fetchedObjects objectAtIndex:indexPath.row ] setValue:@"1" forKey:@"lido"];

    NSError*error;

    // Escreve no banco a alteração
    [context save:&error];

    for (int index = 0; index<[fetchedObjects count]; index++) {

        NSLog(@"%@",[[fetchedObjects objectAtIndex:index]valueForKey:@"lido"]);

.
.
.
// Implementation

}




-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.
.
.

//Implmentation

.
.
.

if ([[arrayProcessosLocal objectAtIndex:indexPath.row ]  valueForKey:@"lido"] == 1) {

        //Torna fonte bold
       cell.textLabel.font = [UIFont boldSystemFontOfSize:17];

      [cell.textLabel setText:[NSString stringWithFormat:@"%@ - %@", [processosLocal valueForKey:@"processo"],[processosLocal valueForKey:@"data_pdf"]]];

        return cell;
    }else{

        // Exibe a fonte normal caso o valor de "lido" seja 0 (zero)        
         [cell.textLabel setText:[NSString stringWithFormat:@"%@ - %@", [processosLocal valueForKey:@"processo"], [processosLocal valueForKey:@"data_pdf"]]];
        return cell;
    }

【问题讨论】:

  • 好的,我会发布代码...
  • 嗨!我贴代码!!

标签: ios objective-c uitableview


【解决方案1】:
  • 您的模型对象应该存储您的消息是否被读取的状态。当邮件未读时,您的cell 应以粗体显示文本。当状态改变时重新加载cell 并让它根据这个状态配置自己。

  • 这是一种非常常见的模式。您在尝试实现它时是否遇到了一些特别的困难?

【讨论】:

  • Yes i have a storage persistence to sabe state of message, when the cell is selected l, her is mark how READ in data storage, but how i implement the cell?
  • 如果您有代码示例,这将有所帮助!我创建了一个代码,我使用 'if()' 来检测 'cellForRowAtIndexPath:' 中的 UNREAD 属性,但这并没有改变。
  • 嘿,我发布了我的代码!看看并说出我做错了什么。
【解决方案2】:

问题是Core Data@"lido" 中的数据类型。对于Core Data@"lido" 字段中的类型是STRING,我正在与IF () 中的INT 进行直接比较。

cellForRowAtIndexPath: 中的代码错误

if ([[arrayProcessosLocal objectAtIndex: indexPath.row] valueForKey: @ "read"] == 1) {
. 
. 
. 
// Other implementation

正确的代码!

if ([[[arrayProcessosLocal objectAtIndex: indexPath.row] valueForKey: @ "read"] isEqual: @ "1"]) {
. 
. 
. 
// Other implementation

Xcode 无法知道来自Core Data 的数据类型,并且在运行时没有问题,因为STRING 类型到if (),等于数值,那么要进行正确的验证必须使用方法isEqual:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 2016-08-11
    • 2018-08-18
    • 2012-02-16
    • 2016-07-22
    • 1970-01-01
    • 2017-01-06
    相关资源
    最近更新 更多