在同一个tableview

时间:2016-10-28 22:00:38

标签: ios objective-c uitableview

我使用的是一种自定义表视图单元格,但是当其他数据发布到我的表视图时,我希望它显示在同一个表中的不同自定义表格视图单元格中。

例如,我在表格视图中创建了一个聊天。但是,当发布某些细节时,我想要一个单独的单元格设计来显示这些细节。到目前为止看到我的代码。

我的问题:如何编写,"如果self.messages中的field_swaptime为空,则显示ChatTableViewCell - 如果它包含数据,则显示SwapDetailTableViewCell" ?

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

    static NSString *ChatTableIdentifier = @"ChatTableViewCell";
    static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell";

    NSDictionary *data = [self.messages objectAtIndex:indexPath.row];


    if ( [data objectForKey:@"field_swaptime"] == nil )  {

     NSLog(@"THIS IS DATA %@", data);


        ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier];
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChatTableViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }



        NSString *userName = [data objectForKey:@"name"];
        [cell.sendingUser setText:userName];

        NSString *messageBody = [data objectForKey:@"body"];
        [cell.messageDisplayed setText:messageBody];

        NSString *timeReceived = [data objectForKey:@"published at"];
        NSLog(@"Message Received at %@", timeReceived);
        [cell.timeStamp setText:timeReceived];




        return cell;
    }

    else {


        SwapDetailTableViewCell *cell = (SwapDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier2];
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SwapDetailTableViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];

        }

        NSString *Time = [data objectForKey:@"field_swaptime"];
        NSLog(@"This is time %@", Time);
        [cell.startTime setText:Time];

        NSString *TimeEnd = [data objectForKey:@"field_endswaptime"];
        [cell.endTime setText:TimeEnd];


                return cell;

    }

}

2 个答案:

答案 0 :(得分:0)

您基本上希望将初始git clone写为:

if

奖励:如果NSDictionary *data = [self.messages objectAtIndex:indexPath.row]; // // if self.messages is nil, then data will be nil and we'll display // a ChatTableViewCell as before. If data is valid, but there is no // value associated with "field_swaptime", then again, we'll display // a ChatTableViewCell. However, if self.messages IS valid and // there IS a value associated with "field_swaptime" key, then the // if clause fails, and we execute the else and return a // SwapDetailTableViewCell. // if ( !data || ![data objectForKey:@"field_swaptime"] } { ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChatTableViewCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; } NSString *userName = [data objectForKey:@"name"]; [cell.sendingUser setText:userName]; NSString *messageBody = [data objectForKey:@"body"]; [cell.messageDisplayed setText:messageBody]; NSString *timeReceived = [data objectForKey:@"published at"]; NSLog(@"Message Received at %@", timeReceived); [cell.timeStamp setText:timeReceived]; return cell; } else { SwapDetailTableViewCell *cell = (SwapDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier2]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SwapDetailTableViewCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; } NSString *Time = [data objectForKey:@"field_swaptime"]; [cell.startTime setText:Time]; NSString *TimeEnd = [data objectForKey:@"field_endswaptime"]; [cell.endTime setText:TimeEnd]; return cell; } 有效且self.messages有效,那么您已经拥有data,并且无需多次拨打data。 (在<{1}}之前,你应<{1>}对[self.messages objectAtIndex:indexPath.row]进行边界完整性检查,但防御性编码部分由你决定。

目标C非常漂亮,因为在nil对象上调用方法,只返回nil。因此,忽略self.messages.count是否有效并且只检索它包含的数据是完全有效的。如果它是有效的(并且您的索引在数组的边界内),那么将返回一个有效的indexPath.row,您可以在整个objectAtIndex:indexPath.row子句中使用它。

答案 1 :(得分:0)

您获得的单元格类型取决于您注册的与标识符对应的类型。因此,当您设置表格时(例如,在viewDidLoad中)......

UINib *nib = [UINib nibWithNibName:@"ChatTableViewCell" bundle:nil];  // bundle:nil means main bundle
[self.tableView registerNib:nib forCellReuseIdentifier:@"ChatTableIdentifier"];

UINib *nib2 = [UINib nibWithNibName:@"OtherTableViewCell" bundle:nil];  
[self.tableView registerNib:nib2 forCellReuseIdentifier:@"OtherIdentifier"];

然后,在cellForRowAtIndexPath中,您使用现代出列方法dequeueReusableCellWithIdentifier:forIndexPath:,它永远不会返回nil并始终返回从您之前注册的nib构建的实例...

// generically
NSString *identifier = (/*some condition*/)? @"ChatTableIdentifier" : @"OtherIdentifier";

// or alternatively, given the condition in your comment...
NSString *identifier = (!data[@"field_swaptime"])? @"ChatTableIdentifier" : @"OtherIdentifier";


UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];

// now cell is one of your two custom classes, built from their respective nibs.

基于相同条件,您可以将cell转换为任意一种,并对其进行配置......

if (/*same condition as identifier*/) {
    ChatTableViewCell *chatCell = (ChatTableViewCell *)cell;
    // do your config for this type here
} else {
    OtherTableViewCell *otherTypeCell = (OtherTableViewCell *)cell;
    // do your config for this other type here
}
return cell;