Как сделать UITableView как ниже изображения? - PullRequest
0 голосов
/ 03 декабря 2011

enter image description here

Как сделать UITableView как на картинке выше?

Я знаю, что это сгруппированный тип таблицы. но как мы можем добавить изображение + метка + кнопка в заголовок раздела.

Я пытался

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

но начинается с CGRectMake(0,0,320,height).

Я хочу чуть выше сечения и точную ширину сечения, как на изображении.

Заранее спасибо.

Ответы [ 4 ]

2 голосов
/ 03 декабря 2011

Вместо того, чтобы пытаться изменить вид заголовка раздела, вы можете создать собственную ячейку с коричневым фоном, меткой и кнопкой и использовать ее для первой строки. Итак, в -cellForRowAtIndexPath вы можете сделать что-то вроде

if (0 == indexPath.row) {
  return brownCell;
} else {
  return normalCell;
}

Существует несколько способов создания пользовательских ячеек, я всегда начинаю с Руководство по программированию табличного представления для iOS .

0 голосов
/ 03 декабря 2011

CustomCell.h

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell{
    id delegate;
    NSIndexPath *indexpath;
}
@property(nonatomic,assign) id delegate;
@property(nonatomic,retain)NSIndexPath *indexpath;
@property(nonatomic,retain) IBOutlet UIToolbar *Toolbar;


-(IBAction)SelectorLeft:(id)sender;
-(IBAction)SelectorRight:(id)sender;
@end

customcell.m

#import "CustomCell.h"
#import <QuartzCore/QuartzCore.h>
@implementation CustomCell
@synthesize Toolbar;
@synthesize delegate,indexpath;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}
-(IBAction)SelectorLeft:(id)sender{
    [delegate perfromselector:@selector(left:) withObject:indexpath];
}
-(IBAction)SelectorRight:(id)sender{
    [delegate perfromselector:@selector(left:) withObject:indexpath];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

UItbaleView part

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

    if (row == 0) {
        CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            self.Cell = nil;
            [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
            cell = self.Cell;
        }
        cell.Toolbar.clipsToBounds=YES;
        CALayer *l=cell.Toolbar.layer;
            // set corner radious
        [l setCornerRadius:10];
            // to apply border on corners
        [l setBorderColor:[[UIColor clearColor] CGColor]];
            // to apply set border width.
        [l setBorderWidth:5.0];

        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
        }
        cell.textLabel.text = [NSString stringWithFormat: @"cell %i",row];
cell.delegate = self;
cell.indexpath = indexpath;
        return cell;

    }
    return nil;
}

Также не забудьте создать Customcell.xib идобавить панель инструментов через конструктор интерфейса, а также создать выход CustomCell в классе таблиц и обработать его, как указано выше

0 голосов
/ 03 декабря 2011
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        //create btn left each on handeled by selector
    UIBarButtonItem *btnleft = [[UIBarButtonItem alloc] initWithTitle:@"List of sms" style:UIBarButtonItemStylePlain target:self action:@selector(ListClicked:)];
     //create btn right each on handeled by selector
    UIBarButtonItem *btnright = [[UIBarButtonItem alloc] initWithTitle:@"New" style:UIBarButtonItemStyleBordered target:self action:@selector(NewClicked:)];
        //create uitoolbar then add btns to it as list of array
    UIToolbar *tool = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        //change style of toolbar to be black
    [tool setBarStyle:UIBarStyleBlack];
    tool.items = [NSArray arrayWithObjects:btnleft,[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],btnright, nil];

        //this is the parent view that we will add on it the uitoolbar objects and the buttons
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [view autorelease];
    [view addSubview:tool];

    return view;
}
-(void)ListClicked:(id)sender{
        //handle here btn left
}
-(void)NewClicked:(id)sender{
    //handle here btn right
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

если вы хотите, чтобы ячейка не находилась в заголовке, вы можете проверить первую строку в cellforrow по пути индекса ... также, если вы хотите сделать это в другом стиле, вы можете создать собственную ячейку и добавить ее также в cellforrowatindexpath

0 голосов
/ 03 декабря 2011
its easy all what you have to

    -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        NSUInteger row = [indexPath row];
        if (row % 2 == 0) {
            static NSString *identifier = @"RowOne";
            UITableViewCell *cell = (RowTypeOne*)[tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell == nil) {
            cell = [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault]autorelease];
            }
            cell.Title.text = [datasource objectatindex:row];
            cell.Title.font = [UIFont fontWithName:@"Tahoma" size:16];
cell.contentView.backgroundColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textAlignment = UITextAlignmentRight;
            return cell;
        }else if (row % 2 == 1) {
            static NSString *identifier = @"RowTwo";
            UITableViewCell *cell = (RowTypeOne*)[tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell == nil) {
            cell = [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault]autorelease];
            }
cell.contentView.backgroundColor = [UIColor redColor];
            cell.Title.text = [datasource objectatindex:row];
            cell.Title.font = [UIFont fontWithName:@"Tahoma" size:16];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textAlignment = UITextAlignmentRight;
            return cell;
        }
        return nil;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...