0

これと似たような質問があることは承知していますが、私のアプローチが異なるので、先に進んで質問します。アプリのログインフォームとして使用する予定のテーブルビューコントローラーがあります。2つのセクションで構成する必要があります。最初のセクションには2つのテーブルセルがあります。最初の行はユーザー名のテキストフィールドで、2番目の行はパスワードのテキストフィールドです。2番目のセクションには、サインインボタンとして機能する行が1つだけあります。ユーザー名とパスワードのセクションを作成することはできましたが、2行目には1行しかないため、実装が少し混乱します。これが私のコードのサンプルです。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}

- (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];

    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

}
if ([indexPath section] == 0)
{ // Email & Password Section
    if ([indexPath row] == 0)
    { // Email
        cell.textLabel.text = @"Username";

    }
    else
    {
        cell.textLabel.text = @"Password";
    }
}

if ([indexPath section] == 1)
{
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.text = @"Sign In to App";
}
return cell;

}

2番目のセクションでは、2つの行が生成されますが、1つだけである必要があります。ありがとうございます。

4

3 に答える 3

3

関数- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionは常に2を返すため、セクションごとに2つの行があります。必要な行数を取得するには、関数にロジックを配置する必要があります。

于 2013-02-12T08:27:49.777 に答える
2

このコードを使用してください-

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
         return 2;
    }
    else
    {
         return 1;
    }  
}
于 2013-02-12T08:48:41.913 に答える
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Foobar"];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Foobar"];

        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }

    if (indexPath.row == 0 && indexPath.section == 0)
    {
        // create and add your userName TextFierld in cell.contentView;
    }
    if (indexPath.row == 1 && indexPath.section == 0)
    {
        // create and add your Password TextFierld in cell.contentView;
    }
    if (indexPath.row == 0 && indexPath.section == 1)
    {
        // create and add your Login UIButton in cell.contentView;
    }
}
于 2013-02-12T08:29:34.803 に答える