0
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

//    NSLog(@"msgcnt123 %@\n",[messageCount objectAtIndex:indexPath.row]);

    NSArray *seperateArray = [[clist objectForKey:[[clist allKeys]objectAtIndex:indexPath.row]]componentsSeparatedByString:@"@"];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    NSLog(@"key %@\n",[[clist allKeys]objectAtIndex:indexPath.row]);

    cell.textLabel.text = [seperateArray objectAtIndex:0];
//    cell.textLabel.text = [contactlist objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

//    NSLog(@"sep %@\n",seperateArray);


    if (![[seperateArray objectAtIndex:1] isEqualToString:@"0"]) {
        NSLog(@"msgCount %@\n",[seperateArray objectAtIndex:1]);
        lblCnt = [[UILabel alloc]initWithFrame:CGRectMake(260, 13, 20, 20)];
        lblCnt.backgroundColor = [UIColor lightGrayColor];
        lblCnt.textColor = [UIColor blackColor];
        lblCnt.text = [seperateArray objectAtIndex:1];
        lblCnt.textAlignment = UITextAlignmentCenter;
        lblCnt.layer.masksToBounds = YES;
        lblCnt.layer.cornerRadius = 2.0f;
        [cell.contentView addSubview:lblCnt];

    }
    else
    {
        NSLog(@"msgCount1 %@\n",[seperateArray objectAtIndex:1]);
        [lblCnt removeFromSuperview];
        lblCnt.hidden = YES;
    }

    return cell;
}

受信したメッセージの数を表示するラベルを各行に追加しました。didSelectメソッドでは、ラベル数をゼロにして、tableViewからラベルを非表示にできるようにします。テーブルビューに複数の行がある場合、ラベルは非表示になりません。

4

1 に答える 1

1

あなたが望むものに到達するための簡単で汚い方法は、タグを使用することです

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

NSArray *seperateArray = [[clist objectForKey:[[clist allKeys]objectAtIndex:indexPath.row]]componentsSeparatedByString:@"@"];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [seperateArray objectAtIndex:0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;

if (![[seperateArray objectAtIndex:1] isEqualToString:@"0"]) {

    lblCnt = [[UILabel alloc]initWithFrame:CGRectMake(260, 13, 20, 20)];
    lblCnt.backgroundColor = [UIColor lightGrayColor];
    lblCnt.textColor = [UIColor blackColor];
    lblCnt.text = [seperateArray objectAtIndex:1];
    lblCnt.textAlignment = UITextAlignmentCenter;
    lblCnt.layer.masksToBounds = YES;
    lblCnt.layer.cornerRadius = 2.0f;
    [cell.contentView addSubview:lblCnt];

    //Add a tag
    lblCnt.tag = 1000;


}
else
{
    /*
    NSLog(@"msgCount1 %@\n",[seperateArray objectAtIndex:1]);
    [lblCnt removeFromSuperview];
    lblCnt.hidden = YES;
     */


    for (UIView *view in [cell.contentView subviews]) {

        if (view.tag == 1000) {
            [view removeFromSuperview];
        }

    }

}

return cell;
}

タグに基づいてビューを選択します。

于 2013-02-04T13:05:26.287 に答える