我在尝试开发一个看似非常简单的应用程序时遇到了困难
基本上,我有一个 UITable 和两个按钮:红色/蓝色
当一个按钮被按下时,带有该按钮相应标题的行被追加到表中
我对 UITableView 必须实现的复杂程度(数据源、委托(delegate)、可重用标识符等)感到不知所措
谁能帮我解决这个问题,最好给我详细的代码
对于我的按钮,我有这样的东西
- (IBAction)buttonPressed:(UIButton *)sender{
NSString *item = sender.currentTitle;
[self.cellArray addObject:item];
[self.myTable reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.cellArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"The Table Cell";
self.myTableCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (self.myTableCell == nil) {
self.myTableCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
self.myTableCell.textLabel.text = [self.cellArray objectAtIndex:indexPath.row];
return self.myTableCell;
}
最佳答案
既然你正在学习这个..我会发布一个简单的解决方案。
make 添加一个成员变量NSMutableArray *cellArrays
;
在你的 viewDidLoad
中初始化它
在buttonPressed
检查;
if([@"red" isEqualToString:[YourButton titleForState:UIControlStateNormal]])
{
[self.cellArrays addObject:@"red"];
}
else
{
[self.cellArrays addObject:@"blue"];
}
[self.YOURTABLEVIEW reloadData];
现在在你的 TableView 数据源方法中
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.cellArrays.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//in between your code;
YOURCELL.textlabel.text = [self.cellArrays objectAtIndex : indexpath.row];
}
试试这个..
关于iphone - 如何在按下按钮时动态更新 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9246862/