多个自定义单元格

时间:2015-07-08 05:53:48

标签: ios objective-c uitableview cell

我正在尝试将我的自定义.XIB Cell称为“ProfileCell1”放入我的表视图的第一个单元格(大小为150),然后将我的第二个自定义.XIB Cell称为“ProfileCell2”放入第二个单元格(大小为60)

我可以将“ProfileCell1”放到第一行,但我无法弄清楚如何将第二行放入,因为第一行将在“numberOfRowsInSection”中从1增加返回值时重复。

到目前为止,这是我的代码:

public function createUser($name, $email, $password,$file_path,$phone,$address,$profession,$language)
    {


        $response = array();

        // First check if user already existed in db
        if (!$this->isUserExists($email)) {

            // Generating API key
            $api_key = $this->generateApiKey();

            // insert query
            $stmt = $this->conn->prepare("INSERT INTO users(name, email, password, api_key,profile_pic,phone,address,profession,language,date) values(?, ?, ?, ?, ?, ?, ?, ?,?,now())");
            $stmt->bind_param("sssssssss", $name, $email, $password, $api_key,$file_path,$phone,$address,$profession,$language);

            $result = $stmt->execute();

            $stmt->close();

            // Check for successful insertion
            if ($result)
            {
                // User successfully inserted
                return USER_CREATED_SUCCESSFULLY;
            }
            else
            {
                // Failed to create user
                return USER_CREATE_FAILED;
            }
        }
        else
        {
            // User with same email already existed in the db
            return USER_ALREADY_EXISTED;
        }

        return $response;
    }

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

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

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

     if (cell == nil)
     {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProfileCell1" owner:self options:nil];
            cell = [nib objectAtIndex:0];
     }

     cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
     return cell;
}

所以我知道这段代码会重复我的ProfileCell1,但我无法弄清楚如何让它只出现一次,然后从第二个单元格启动我的ProfileCell2,让两个单元格在'heightForRowAtIndexPath中返回2个不同的值

我尝过这样的话:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
      return 150;
}

但它似乎不起作用。干杯!

4 个答案:

答案 0 :(得分:1)

试试这种方式

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section==0) {
    return 1;// your require number of cell
}
else
{
   return 1;
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        static NSString *identifier = @"ProfileCell1";
        ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];

        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProfileCell1" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }

        cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
        return cell;
    }
    else
    {
        // your second cell coding here
        static NSString *identifier = @"ProfileCell2";
        ProfileCell2 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];

        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProfileCell2" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
        // your second cell code 
        //cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
        return cell;
    }
    }

并增加单元格的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return 150;
    }
    else
    {
         return 60;
    }
}

您可以使用两个部分在同一UITableView中使用两个不同的单元格。第一部分使用第一个单元格.xib,第二部分使用第二个.xib文件。

答案 1 :(得分:1)

您还可以在单​​cells中使用两个不同的section。对Dharmesh Dhorajiya的帖子做一些编辑。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        static NSString *identifier = @"ProfileCell1";
        ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];

        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProfileCell1" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }

        cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
        return cell;
    }
    else
    {
        // your second cell coding here
        static NSString *identifier = @"ProfileCell2";
        ProfileCell2 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];

        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProfileCell2" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }

        cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
        return cell;
    }
    }

并使用相同的代码返回两个不同单元格的高度。

if (indexPath.row == 0) {
    return 150;
else {
    return 60;
}

答案 2 :(得分:0)

如何识别要放置 ProfileCell1 ProfileCell2

indexPath
Error running command: "ctmpsm -UPDATEAJF 0e4ba HOLD" see above shell error
Return code: 1
Error running command: "lsl" see above shell error
Return code: 127

答案 3 :(得分:0)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (indexPath.row == 0) 
    return 150;
  else 
    return 60;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
   if (indexPath.row == 0) 
    {
      static NSString *identifier = @"ProfileCell1";
      ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];
      //bla bla bla code for ProfileCell1 here
    return cell;
   }
   else
   {
      static NSString *identifier = @"ProfileCell2";
      ProfileCell2 *cell = (ProfileCell2 *)[tableView dequeueReusableCellWithIdentifier:identifier];
      //bla bla bla code for ProfileCell2 here
    return cell;
   }

}

相关问题