2011-06-23 23 views
5

Mam UITableView ze specjalnym wierszem na końcu, aby wstawić nowy element. To działa, ale chcę, aby miała zieloną ikonę plusa bez konieczności przełączania widoku tabeli w tryb edycji. Jak mogę to zrobić?Sposób umieszczenia zielonej ikony Dodaj bezwarunkowo w wierszu Wstaw

Wolałbym nie tworzyć przycisków ani grupować obrazu, jeśli to możliwe. Czy istnieje sposób na wykonanie jednej lub obu tych rzeczy przy użyciu tylko standardowych funkcji UITableView/UITableViewCell?

+0

Czy używasz komórki niestandardowej? ? – Jhaliya

+0

To zwykły UITableViewCell. –

+0

masz na myśli, że nie chcesz widoku tabeli w trybie edycji, czy nie chcesz, aby użytkownik musiał przejść do trybu edycji? programowo ustawianie widoku tabeli tak, aby zawsze był w trybie edycji, służy twoim celom? – bshirley

Odpowiedz

0

Niestety, jedynym sposobem, w jaki to zrobiłem, jest ustawienie obrazu komórki, co oznacza, że ​​musisz sam sobie poradzić z plikami obrazów, zamiast ładować je dla UIKit. Zalecam użycie UIKit Artwork Extractor, aby uzyskać obraz.

1

Możesz zaimplementować swoją ostatnią komórkę jako Niestandardową komórkę i dodać zieloną ikonę zgodnie z własnym wyborem.

Zobacz samouczek, aby zaimplementować komórkę niestandardową.

iPhone Programming Tutorial: Part 6: Creating custom UITableViewCell Using Interface Builder UITableView

Aktualizacja:

Powiedzmy komórka jest instancją UITabelViewCell.

Najpierw utwórz przycisk za pomocą zielonej ikony.

UIButton myGreenIconButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[myGreenIconButton addTarget:self action:@selector(GreenIconButtonClicked:)forControlEvents:UIControlEventTouchUpInside]; 
[myGreenIconButton setBackgroundImage:[UIImage imageNamed:@"greenIcon.png"] forState:UIControlStateNormal]; 
myGreenIconButton.tag = i; 
myGreenIconButton.backgroundColor = [UIColor clearColor]; 
myGreenIconButton.frame = CGRectMake(5, 78, 15, 15); 

Teraz dodać go jako podrzędny w ostatniej UITabelViewCell.

[cell addSubview:myGreenIconButton]; 

Wdrożenie GreenIconButtonClicked: metodę otrzymywać kliknij evrnt na dodaniu zielony przycisk ikona

-(void) GreenIconButtonClicked:(id) sender 
{ 

} 
+0

@Peter Hosey: możesz dodać zielony pakiet ikon z 'UIButton' w twoim' UITabelView' jako 'subview'. – Jhaliya

+0

@Peter Hosey:: Sprawdź zaktualizowaną odpowiedź. – Jhaliya

2

chcesz ustawić accessoryView do komórki:

@interface RootViewController : UITableViewController { 
    NSInteger nextValue; 
    NSMutableArray *timeIntervals; 
} 


@implementation RootViewController 


- (NSNumber *)nextValue { 
    NSNumber *n = [NSNumber numberWithInteger:nextValue]; 
    nextValue++; 
    return n; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    nextValue = 1; 
    timeIntervals = [[NSMutableArray alloc] init]; 
    [timeIntervals addObject:[self nextValue]]; 
} 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section { 
    return [timeIntervals count]; 
} 

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

    static NSString *CellIdentifier = @"TimeIntervalCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
             reuseIdentifier:CellIdentifier] autorelease]; 
     UIButton *b = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
     [b addTarget:self action:@selector(addTapped:) forControlEvents:UIControlEventTouchUpInside]; 
     cell.accessoryView = b; 
    } 

    NSNumber *number = [timeIntervals objectAtIndex:indexPath.row]; 
    cell.accessoryView.tag = indexPath.row; 
    cell.textLabel.text = [number stringValue]; 
    cell.detailTextLabel.text = @"detail about this number"; 
    return cell; 
} 

- (void)addTapped:(UIButton *)sender { 
    id cell = sender; 
    while (cell != nil && [cell isKindOfClass:[UITableViewCell class]] == NO) 
    cell = [cell superview]; 

    if (cell == nil) { 
    NSLog(@"[%@ %@] sender was not in a cell", 
      NSStringFromClass([self class]), NSStringFromSelector(_cmd)); 
    return; 
    } 

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
    NSInteger index = indexPath.row + 1; // insert after current cell 
    [timeIntervals insertObject:[self nextValue] atIndex:index]; 
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:index inSection:0]; 
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 
         withRowAnimation:UITableViewRowAnimationFade]; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"[%@ %@] not implemented", NSStringFromClass([self class]), NSStringFromSelector(_cmd)); 
} 


@end 

(to wszystko zmodyfikowany kod do szablonu aplikacji nawigacyjnej Xcode 4.0.2)

Powiązane problemy