将联系人以字母和分组格式加载到UITableView

时间:2014-01-04 20:11:52

标签: android ios objective-c uitableview

自定义表格视图单元格的外观。

以字母分组格式将联系人加载到UITableView

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

    Contacts *contact = [[self.contactsSections valueForKey:[[[self.contactsSections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
    cell.backgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"contacts_bar.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];

    // setting the Label
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(winSize.width/4.57, 0, winSize.width/1.6, cell.frame.size.height)];
    [textLabel setBackgroundColor:[UIColor clearColor]];
    textLabel.text = contact.firstname;
    [cell.contentView addSubview:textLabel];

    // creating the contact imageView
    UIImageView *contctImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, winSize.width/4.9, winSize.width/4.9)];
    contctImageView.image = [UIImage imageNamed:@"contact_pic.png"];
    contctImageView.contentMode = UIViewContentModeScaleToFill;
    [cell.contentView addSubview:contctImageView];

    return cell;
}

我需要UI设计Tableview.Image和联系人名称的每个单元格,如android联系人设计。任何人都可以帮助我解决上述代码中的问题。

1 个答案:

答案 0 :(得分:0)

您不需要将imageView和标签添加到UITableViewCell,只需使用该单元具有的那些:

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

        Contacts *contact = [[self.contactsSections valueForKey:[[[self.contactsSections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
        cell.backgroundView =  [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"contacts_bar.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];

        // setting the Label
        cell.textLabel.text = contact.firstname;

        // creating the contact imageView
        cell.imageView.image = [UIImage imageNamed:@"contact_pic.png"];

        return cell;
    }

如果您正在寻找如何实现自定义单元格,请参阅:

http://www.appcoda.com/customize-table-view-cells-for-uitableview/

相关问题