2015-06-09 60 views
0

i。我嘗試了很多,但無法成功執行。二,在tableview單元格中,有3 3個字段需要顯示。一個圖像視圖,button1 - >拍照按鈕,按鈕2 --->瀏覽按鈕。 iii。第一次tableview應該顯示一行自定義單元格。 iv。當用戶點擊位於tableview之外的「添加新按鈕」時,將創建一個新行,所有以上3個字段(圖像視圖,按鈕1,按鈕2) v。「添加新按鈕」的點擊次數,將創建新的行數超過3個字段。 vi。我可以通過包含上述3個字段的簡單圖像視圖成功動態創建上述所有內容,但無法成功處理自定義單元格。通過在單個表格視圖中按鈕點擊來動態添加tableview單元格

vii。再次,我需要設置每個單元格的標籤,broswe按鈕,拍照按鈕,以便點擊時,將採取標籤值。

+0

你需要更清楚。請參閱此處發佈的其他問題並重構您的問題。 – Adithya

回答

2

表視圖通過添加委託和數據源來工作。假設你的表視圖擁有一個所有者作爲視圖控制器,並且委託和數據源都是視圖控制器本身。所有你需要做的就是實現這些數據源方法來返回一個合適的數據,那麼你應該在表視圖上調用reloadData,或者如果你想多做一些額外的工作來查看更好的檢查如何添加動畫周圍的網絡行。

這是一個非常簡單但沒有優化的例子,但非常簡短,易於閱讀。我希望它能幫助你走上正確的軌道:

@interface MyViewController()<UITableViewDataSource, UITableViewDelegate> 

@property UITableView *tableView; 
@property NSArray *myCells; 

@end 

@implementation MyViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.tableView.delegate = self; // could be done in storyboard 
    self.tableView.dataSource = self; // could be done in storyboard 
    [self addACell]; 
} 
- (void)addCellButtonPressed:(id)sender { 
    [self addACell]; 
} 
- (void)addACell { 
    MyCell *cell = [[MyCell alloc] init]; 
    [cell.button1 addTarget:self action:@selector(cellButton1Pressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.button2 addTarget:self action:@selector(cellButton2Pressed:) forControlEvents:UIControlEventTouchUpInside]; 
    self.myCells = [self.myCells arrayByAddingObject:cell]; 
    [self.tableView reloadData]; // will call the delegate again and refresh cells 
} 
- (void)cellButton1Pressed:(id)sender { 
    MyCell *cellPressed = nil; 
    for(MyCell *cell in self.myCells) { 
     if(cell.button1 == sender) { 
      cellPressed = cell; 
      break; 
     } 
    } 
    // do whatever 
} 
- (void)cellButton2Pressed:(id)sender { 
    MyCell *cellPressed = nil; 
    for(MyCell *cell in self.myCells) { 
     if(cell.button2 == sender) { 
      cellPressed = cell; 
      break; 
     } 
    } 
    // do whatever 
} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return self.myCells.count; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return self.myCells[indexPath.row]; 
} 

@end 
相關問題