点击按钮时改变tableview中其中一行的文本

qscool1987 2012-12-12 08:58:37
如题,相关代码如下

//在按下该按钮时要求改变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中的文本一直没有改变,弄了一下午了,求解决方法
...全文
433 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Zeteyu 2012-12-27
  • 打赏
  • 举报
回复
建议看下 lazytable的列子
zhsl 2012-12-27
  • 打赏
  • 举报
回复
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; UITableViewCell *cell = [tableCMDView cellForRowAtIndexPath:indexPath]; cell.textLabel.text = @"123"; [cell setNeedDisplay]; 试试
ReyZhang 2012-12-21
  • 打赏
  • 举报
回复
按你写的代码,应该说是可以的。但不知道你那边为什么看不到效果。 不过还有一种方法可以偿试,就是:修改指定行的数据源,然后再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]; }
qscool1987 2012-12-12
  • 打赏
  • 举报
回复
引用 1 楼 jiangyuaiqing 的回复:
你应当改变你dataSource中对应该行的数据,然后使用 - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0); 这个方法来reload该行
不能直接这样改吗?

- (IBAction)inputCMDEnter:(UIButton *)sender {  
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    UITableViewCell *cell = [tableCMDView cellForRowAtIndexPath:indexPath];
    cell.textLabel.text = @"123";
}
我看到apple提供的一个示例代码就是这样写的。那个工程可以这样找到,打开xcode帮助文档,搜索uitableview,左下角那个datecell就时了。麻烦帮忙看一下,不知道apple是怎么做到的。我刚开始写这种程序,不太懂。
Snake_Jay 2012-12-12
  • 打赏
  • 举报
回复
你应当改变你dataSource中对应该行的数据,然后使用 - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0); 这个方法来reload该行
ocrazymumu 2012-12-12
  • 打赏
  • 举报
回复
哇哇哇 好东西
相奇 2012-12-12
  • 打赏
  • 举报
回复
引用 2 楼 qscool1987 的回复:
引用 1 楼 jiangyuaiqing 的回复:你应当改变你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 又会 执行一边 取数值操作。。。。很令人蛋疼得。 }
Snake_Jay 2012-12-12
  • 打赏
  • 举报
回复
不介意的话你把CODE发我给你看一下,supermaxdev@gmail.com

29,030

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧