繁体   English   中英

如何以编程方式在分成几部分的静态表格中设置单元格

[英]How to setup cells in a static table divided in sections programmatically

我基本上是在我的应用程序上进行设置视图,并且为此使用了静态表。 因此,我将表格分为3个部分,每个部分有一个单元格。 不是以编程方式,我可以轻松标记每个单元格,并且它可以工作,但是以编程方式,我无法初始化每个单元格。 我只能初始化在3个部分中重复出现的第一个单元格。 我想为每个部分初始化一个单元格的方法,但是我找不到一种方法或一种方法来做到这一点。 我的表视图还具有reuseIdentifiers,但似乎UITableViewController不能识别它。

到目前为止,这是我所做的。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 1;
}

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

    // Configure the cell...


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        // More initializations if needed.
    }
    //Observations *currentList = [self.teacherNames objectAtIndex:indexPath.row];
    cell.textLabel.text = @"Hey";
    //return cell;

    return cell;
}

但是我想要的是第一部分中的单元格被标记为:Edit Profile,第二部分为Invite,第三部分为Logout

您必须为如何处理表格视图中的每个不同部分创造条件。

if (indexPath.section == 0) {
    cell.textLabel.text = @"Edit Profile";
}else if (indexPath.section == 1) {
    cell.textLabel.text = @"Invite";
}else if (indexPath.section == 2) {
    cell.textLabel.text = @"Log out";
}......

查看tableView委托方法:

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

(如果要标记段标题而不是段本身中的单元格,则为标签)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM