0

tableViewにtextFieldsが必要でした。そこで、ストーリーボードのtableViewに手動で挿入し、個々のプロパティを変更しました。しかし、プログラムを実行したときにテキストフィールドが表示されませんでした。スイッチを使って同じ手順を繰り返しました。しかし、それでも実行されませんでした。そのため、ストーリーボードにオブジェクトが存在するにもかかわらず、オブジェクトが表示されていないことがわかりました。

Q1。なぜこれが当てはまるのか、何か考えはありますか?

この問題を解決するために、これらのオブジェクトをプログラムで挿入しました。UISwitchの場合、cellForRowAtindexPath直後に次のコードを追加しましたinitWithStyle:style reuseIdentifier:reuseIdentifier

UISwitch *newSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(25, 55, 77, 27)];
[newSwitch addTarget: self action: @selector(pictureSwitchAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:newSwitch]; 

これは正常に機能します。pictureSwitchActionメソッドは期待どおりに呼び出されます。

ただし、UITextFieldで同様のことを行おうとすると、アプリがクラッシュします。(FYI ...field0はとして宣言されていpropertyます。)これが私のコードです:

field0 = [[UITextField alloc] initWithFrame:CGRectMake(0, 50, 320, 44)];   // (1)
cell = (UITableViewCell *)[field0 superview];                              // (2)
[self.view addSubview:field0];                                             // (3)

(1)と(2)(コメント(3))はアプリをクラッシュさせます。エラー:*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

私の理解では、セルに値を割り当てる前に、セルを最初に返す必要があります。ただし、(1)と(3)((2)のコメント)では、結果は得られません。これがUITextFieldではなくスイッチで機能する理由がわかりません。

Q1への回答と、プログラムでtextFieldを挿入する方法はありますか?

ありがとう!

編集1

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    cell.selectionStyle = UITableViewCellSelectionStyleNone; // VERY IMPORTANT

    if(indexPath.section == 2)
    {
        if (indexPath.row == 0)
        {
            field0 = [[UITextField alloc] initWithFrame:CGRectMake(0, 50, 320, 44)];
            cell = (UITableViewCell *)[field0 superview];
           // [self.view addSubview:field0];
        }

        if (indexPath.row == 1)
        {
            field1 = [[UITextField alloc] initWithFrame:CGRectMake(0, 50, 300, 43)];
            cell = (UITableViewCell *)[field1 superview];            
            // [self.view addSubview:field1];
        }

       if (indexPath.row == 2)
       {
            field2 = [[UITextField alloc] initWithFrame:CGRectMake(0, 50, 300, 43)];
            cell = (UITableViewCell *)[field2 superview];
            // [self.view addSubview:field2];
        }
    }

    return cell;
}
4

1 に答える 1

1

ここでは、Asのようにコードを変更する必要があります

回答-1:-ビューを追加するだけでセルをカスタマイズする場合は、セルをコンテンツビューに追加する必要があります。これにより、セルが編集モードに移行したり、編集モードから移行したりするときに、セルが適切に配置されます。

Answer-2クラッシュの理由:-UITableViewCellを返さなかったため。クラッシュログItSelfが同じことを説明している*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

回答3-これMy understanding is that the cell has to be return first before I can allocate some value to itについて。こここの行については、あなたが完全に間違っていると思います。can you put mango inside the basket even if you don't have that basket.means you will need thattextfieldしたがって、最初にTableCell必要なものを追加するときはいつでも同じ概念がここにcreate(allocate)あり、次にその上に任意のオブジェクトを追加して、最後にメソッド内に表示されるようにそれtableCellを返すことができます。私はあなたが今クリアしたと思います。tableCellCellForRowAtIndexPath

UISwitchの場合は、self.view手段を介して追加していましたcontroller's view。コード[self.view addSubview:]を参照して、mainView(conrtoller'view)を切り替えるのではなく、Shwoingを実行します。TextFieldのtableViewCell場合は、この行が原因で機能しませんでした。これはcell = (UITableViewCell *)[field0 superview];完全に間違っています。

あなたの親切な情報のためだけに、以下のコードを参照してください。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

cell.selectionStyle = UITableViewCellSelectionStyleNone; // VERY IMPORTANT

if(indexPath.section == 2)
{

    if (indexPath.row == 0)
    {
        UITextField* thisTextField =[[UITextField alloc] initWithFrame:CGRectMake(originX, originY, width ,hieght)];//pass the co-ordinate.
        [thisTextField setBackgroundColor:[UIColor clearColor]];
        [thisTextField setTag:indexPath.row];//here by setting this you can access further it         
        thisTextField.delegate= self;
        [[cell contentView] addSubview:thisTextField];
        // If you want to customize cells by simply adding additional views, 
        // you should add them to the content view .
        //  so they will be positioned appropriately as the cell transitions into and out of editing mode.
    }

return cell;

}

ビュー階層とUITableViewの基本から始めることをお勧めします。

ありがとう

于 2012-11-16T20:05:29.037 に答える