2 кнопки рядом в UITableViewCell - PullRequest
       0

2 кнопки рядом в UITableViewCell

0 голосов
/ 10 ноября 2011

В приложении Twitter для iPhone в представлении «Мой профиль» есть раздел с двумя строками, где каждая статистика, фолловеры, твиты и избранные были представлены, каждая из которых, похоже, имеет эффект UIButton. Теперь мне интересно, как это было создано - используя изображения для каждой кнопки? Чтобы быть более понятным, это что-то вроде следующего.

|-----------------------|
|   23       |    11    |
| Following  |  tweets  | <-- tableview cell 1
|            |          |
|-----------------------|
|    3       |    11    |
| Followers  |favorites | <--- tableview cell 2
|            |          |
|-----------------------|

1 Ответ

0 голосов
/ 10 ноября 2011

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

@interface CustomCell : UITableViewCell {
UIButton* leftButton;
UIButton* rightButton;
}
@property(nonatomic, retain) IBOutlet UIButton* leftButton;
@property(nonatomic, retain) IBOutlet UIButton* rightButton;
@end

@implementation CustomCell

@synthesize leftButton, rightButton;

-(id) initWithFrame:(CGRect)frame reuseIdentifier:(NSString*)reuseIdentifier {
    if( self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier] ) {
        // init
    }
    return self;
}

-(void) setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // configure for selected
}

- (void)dealloc {
    [rightButton release];
    [leftButton release];
    [super dealloc];
}

@end

// in root view controller:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CustomCellIdentifier";

    // Dequeue or create a cell of the appropriate type.
    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        for( id oneObject in nib ) {
            if( [oneObject isKindOfClass:[CustomCell class]] ) cell = (CustomCell*)oneObject;
        }
    }

    NSInteger row = indexPath.row;

    UIButton* tempButton = cell.leftButton;
    tempButton.tag = row * 2;
    [tempButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [tempButton setBackgroundImage:[thumbnails objectAtIndex:(row * 2)] forState:UIControlStateNormal];

    tempButton = cell.rightButton;
    tempButton.tag = (row * 2) + 1;
    [tempButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [tempButton setBackgroundImage:[thumbnails objectAtIndex:((row * 2) + 1)] forState:UIControlStateNormal];

    return cell;
}

Этот код немного старше, но он демонстрирует смысл.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...