我知道有与此类似的问题,但由于我的方法不同,我将继续提问。我有一个 tableview Controller ,我打算将其用作我的应用程序的登录表单。它应该由两部分组成;第一部分有两个表格单元格;第一行是用户名文本字段,第二行是密码文本字段,第二部分只有一行作为登录按钮。我已经能够实现用户名和密码部分,但是由于第二行只有一行,所以实现起来有点困惑。这是我的代码示例;
- (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;
第二部分产生两行,本应只有一行,请帮忙谢谢。
最佳答案
函数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
总是返回 2,所以每个部分都有两行。您应该将逻辑放入函数中以获取所需的行数。
关于iphone - 使用 xcode 和 tablecells 登录表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14828313/