读书人

点击旋钮时改变tableview中其中一行的

发布时间: 2013-02-17 10:44:46 作者: rapoo

点击按钮时改变tableview中其中一行的文本
如题,相关代码如下


//在按下该按钮时要求改变tableview其中一行的文本
- (IBAction)inputCMDEnter:(UIButton *)sender {
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell *cell = [tableCMDView cellForRowAtIndexPath:indexPath];
cell.textLabel.text = @"123";
}

//代理相关的函数
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return 15;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"COMMANDS";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

在connections inspecors中,已经将tableview的datasource和delegate关联到files owner了。
可是tableview中的文本一直没有改变,弄了一下午了,求解决方法
[解决办法]
你应当改变你dataSource中对应该行的数据,然后使用
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);

这个方法来reload该行
[解决办法]
不介意的话你把CODE发我给你看一下,supermaxdev@gmail.com

[解决办法]
引用:
引用:你应当改变你dataSource中对应该行的数据,然后使用
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);

……



你设定了 新得数值,最好是 让 tableView 重新 绘制一下(可以整个表 重绘reloaddata,也可以是section,也可以是row)
而且,重绘得时候,会调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

这里,你得cell.textlabei.text 又会 执行一边 取数值操作。。。。很令人蛋疼得。

[解决办法]
按你写的代码,应该说是可以的。但不知道你那边为什么看不到效果。
不过还有一种方法可以偿试,就是:修改指定行的数据源,然后再reloadRow

- (IBAction)inputCMDEnter:(UIButton *)sender {
int rowIndex=0;
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:rowIndex inSection:0];
NSString *title=[listData?objectAtIndex:rowIndex];
title=@"123";
[listData replaceObjectAtIndex:indexPath withObject:title];



[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:YES];

}

读书人网 >Iphone

热点推荐