0

私のアプリでは、4つのUITextViewを使用してUITableViewセルをカスタマイズしています。テーブルビューにデータを追加してリロードするたびに。UITableViewCellのテキストは、前のテキストで上書きされます。

さまざまなアプローチを試しましたが、何が問題なのか理解できませんでした。

ViewControllerでTableViewを使用しています。

テーブルビューセルで使用したコードは次のとおりです。

if ( [tableView isEqual:self.tableActions])
{
    //Setting the text empty

    static NSString *CellIdentifier = @"ActionsIdentifier";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    }
    NSLog(@"Index Path %i",[indexPath row]);
    ActionDetails *actiondetails = [actionArray objectAtIndex:[indexPath row]];

    NSLog(@"Action Text %@",actiondetails.actions);

    //Actions
    actionText=[[UITextView alloc] initWithFrame:CGRectMake(10, 5, 230,30)];
    actionText.font = [UIFont systemFontOfSize:17.0];
    actionText.editable = NO;
    actionText.textColor=[UIColor blackColor];
    actionText.text = actiondetails.actions ; 
    actionText.userInteractionEnabled = NO;
    actionText.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:actionText];


    //Owner         
    ownerBy=[[UITextView alloc] initWithFrame:CGRectMake(230, 5, 230,30)];
    ownerBy.font = [UIFont systemFontOfSize:17.0];
    ownerBy.textColor=[UIColor blackColor];
    ownerBy.textAlignment = UITextAlignmentCenter;
    ownerBy.text = actiondetails.owner;
    ownerBy.editable = NO;
    ownerBy.userInteractionEnabled = NO;
    ownerBy.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:ownerBy];

 }

ScreenShot ここに画像の説明を入力してください

助けてくれてありがとう。

とても有難い。

4

7 に答える 7

2

これは先日私に起こりました。私が思いついた解決策は、ifステートメントでセルを作成した後にセルからすべてのサブビューを削除することでした。

if(cell == nil)
{
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
if ([cell.contentView subviews]){
    for (UIView *subview in [cell.contentView subviews]) {
        [subview removeFromSuperview];
    }
}
于 2012-10-05T11:36:31.003 に答える
1

Tableviewのセル識別子を削除します。それ以外の場合は、TableviewのCustomcellを使用します。

于 2012-10-05T11:20:50.237 に答える
1

この方法を試してみてください:

 {
        //Setting the text empty

        static NSString *CellIdentifier = @"ActionsIdentifier";

        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        **// if(cell == nil) // comment this line in your code ,its work 
       // {**
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
       **// }**
        NSLog(@"Index Path %i",[indexPath row]);
        ActionDetails *actiondetails = [actionArray objectAtIndex:[indexPath row]];

        NSLog(@"Action Text %@",actiondetails.actions);

        //Actions
        actionText=[[UITextView alloc] initWithFrame:CGRectMake(10, 5, 230,30)];
        actionText.font = [UIFont systemFontOfSize:17.0];
        actionText.editable = NO;
        actionText.textColor=[UIColor blackColor];
        actionText.text = actiondetails.actions ; 
        actionText.userInteractionEnabled = NO;
        actionText.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:actionText];


        //Owner         
        ownerBy=[[UITextView alloc] initWithFrame:CGRectMake(230, 5, 230,30)];
        ownerBy.font = [UIFont systemFontOfSize:17.0];
        ownerBy.textColor=[UIColor blackColor];
        ownerBy.textAlignment = UITextAlignmentCenter;
        ownerBy.text = actiondetails.owner;
        ownerBy.editable = NO;
        ownerBy.userInteractionEnabled = NO;
        ownerBy.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:ownerBy];

     }
于 2012-10-05T11:27:21.133 に答える
1

セルを再利用しています。再利用されたセルにはすでにUITextviewが追加されているため、オーバーライドしています。UITextViewの作成と追加のすべてのコードをにシフトする必要があります。

If(cell == nil){

}

その後、テキストをUITextviewに設定するだけで済みます。

于 2012-10-05T11:32:32.177 に答える
0

ビューが表示されたら、テーブルビューを書き込みリロードします。

-(void)viewDidAppear:(BOOL)animated {    
[tableView reloadData]; 
}
于 2012-10-05T11:33:24.963 に答える
0

cellForRowAtIndexPathメソッドを変更する

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

        NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

           // your code 

        }

          // your code 

        return cell;    

    }
于 2012-10-05T11:40:48.173 に答える
0
for (id vw in [[cell contentView] subviews]) {
    if ([vw isKindOfClass:[UILabel class]])
    {
       UILabel *label = (UILabel *)vw;
       [label removeFromSuperview];
    }
 }

 for (id vw in [cell subviews]) {
     if ([vw isKindOfClass:[UILabel class]])
     {
        UILabel *label = (UILabel *)vw;
        [label removeFromSuperview];
     }
 }
于 2014-07-10T07:15:08.437 に答える